I’m using ASP.NET MVC 3 and Entity Framework 4.1.
I was wondering what is the preferred method of updating a object when not all of the properties are provided in the HTTP Post.
For example, an Order object may have the properties of Items, CreateDate and UpdateDate. In an edit form only the Items property will be entered and posted to the Edit ActionMethod. So the below basic code will fail as the CreateDate and UpdateDate properties are not included with the order.
[HttpPost]
public ActionResult Edit(Order order)
{
{
db.Entry(order).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(order);
}
What is the best way to handle this situation? For simple objects such as this order I suppose the CreateDate and UpdateDate can be kept in hidden fields, however, for more complex objects (such as those with several one-to-many relationships) should the object id be used to retrieve the full object and then overwrite some of its properties with the values posted back in the form…
One option is to create view models
You can use AutoMapper to map them
Other option is to retrieve the object from database and update it