MVC2 RTM - model binding complex objects using Entity Framework

前端 未结 2 1383
情深已故
情深已故 2021-01-25 11:36

I am new to MVC, and am really struggling with what I seems like it should be a very common scenario. I\'m using MVC2 RTM, and the Entity Framework for my model objects.

<
2条回答
  •  清酒与你
    2021-01-25 12:06

    Ultimately I found a more elegant solution, but never came back to post it. Here it is - sorry for the delay:

    if (ModelState.IsValid)
            {
                if (TryUpdateModel(parent, new[] "prop1", "prop2", "prop3" })) //specify parent-only properties to include
                {
                    if (TryUpdateModel(parent.ChildObjects, "ChildObjects"))
                    {
                        _ef.SaveChanges();
                        return RedirectToAction("Details", new { id = parent.ID })                    }
                }
            }
            return View(parent);
    

    I'm converting this code from a real life app, so my apologies for any typos.

提交回复
热议问题