Models is populated within the ctor.
When I click Edit to edit an item ,it all works, and I can clearly see that the Model has been updated within Models after the TryUpdateModel() call.
However when its redirected to the index, Models doesn’t have my changes any longer. What happened?
// GET: /Contact/
public ActionResult Index()
{
return View(Models);
}
// GET: /Contact/Edit/5
public ActionResult Edit(int id)
{
var contactModel = Models.Find((x) => x.ID == id);
return View(contactModel);
}
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
var contactModel = Models.Find((x) => x.ID == id);
if (TryUpdateModel(contactModel))
{
return RedirectToAction("Index");
}
return View(contactModel);
}
I believe you only changed the model in memory from the Edit call, but not saved the changes down to persistant storage (database), so your ctor is reloading it from an unsaved state.