mvc 4 using sql server 2008, code first, ef5
I have a ViewModel associated with a view.
As a starting point I'm modifying the scaffolding created by VS.
I can populate the form fine.
When the user clicks save - this is the code in the scaffolding:
[HttpPost]
public ActionResult Edit(Place place)
{
if (ModelState.IsValid)
{
db.Entry(place).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(place);
Place is a model. I need to pass a viewmodel (including place) back to this.
How do I persist the viewmodel in the database using EF? Do I have to split it into models first?
When I try that I get this error:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
When I debug it also seems the PlaceID (from model) is missing from the model that's passed back to the controller httpPost Edit controller - maybe that's the source of the error?
Are there any examples of best practice online for this - I can't find anything.
Thanks.