views:

49

answers:

2

Hi,

I have a simple form which inserts a new Category with the given parentID (ServiceID).
and my parent child relationship is this;

Service > Category

my url for creating a Category based on the ServiceId is this
/Admin/Categories/Create/3 => "3 is the serviceID"

and my Action method is this

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(int? id, Category category)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    category.Service = dbService.GetAll().WithServiceId(id.Value).SingleOrDefault();
                    dbCategory.Add(category);
                    dbCategory.Save();
                    return RedirectToRoute("List_Categories", new { ServiceId = id.Value });
                }
                catch
                {
                    ModelState.AddRuleViolation(category.GetRuleViolations());
                }
            }
            return
                View ....

I am using LinqToSql for db actions. Anyway the strange part begins here. When i save the data it inserts a data to Service table instead of insterting data just to Category Table and the data which is inserted to Category Table has the new inserted ServiceID.

Is it a problem with the ASP.NET MVC ModelBinder or am i doing a mistake that i dont see ?
I have used LinqToSql in several projects and have never had an issue like this

+1  A: 

It could be an error in your LINQ To SQL set up. Is the service id an autogenerated field in the DB -- is it marked as such in the LINQ to SQL classes? Does your service disconnect it from the data context, then not reattach it before submitting? If so, have you tried adding the category to the service and then saving the updated service?

  var service = ...
  service.Categories.Add(category);
  service.Save();
tvanfosson
Yes, the ServiceID is autogenerated and it is marked in the LinqToSQL classes. And before saving the category it is disconnected from the datacontext. I pull the service from db for Categor.Service
Barbaros Alp
I have tried adding a Category via service and it worked.But when i try to save a category with the parent entity it inserts a new Service and category with the new added serviceID
Barbaros Alp
I suspect that it doesn't know that it is the same service as what was retrieved. It needs to be reattached to the data context before trying to save the category.
tvanfosson
+1  A: 

Where does your Category object come from? As I see you are relying on ASP.NET MVC Model binding for providing it but for that you need a form somewhere to make a POST. It would help to see the form too.

Also personally I rather use FormCollection (rather than business model classes) in my action signatures and then pull the posted data out from the FormCollection by myself. Coz sooner or later you end up having some complex objects with various one-to-many, many-to-many relationships that Model Binding just won't pick up and you have to construct it by yourself.

mare
Thanks for your answer, whatever i did i couldnt solve it. It is about datacontext. Instead of doing this <br /> category.Service = dbService.GetAll().WithServiceId(id.Value).SingleOrDefault();i did this <br />category.ServiceID = id.Value;
Barbaros Alp