If I have the usual Edit actions, one for GET to retrieve the object by its identifier and display it in the edit form. The following is for POST to accept values in the ViewModel and update the object in the database.
public virtual ActionResult Edit(int id)
[HttpPost]
public ActionResult Edit(VehicleVariantEditSaveViewModel viewModel)
If an error occurs during model binding in the POST action, I understand that I can return the RedirectToAction to the GET action and save the ModelState validation errors by copying it to TempData and retrieving it after the redirect in the GET action.
if (TempData["ViewData"] != null)
{
ViewData = (ViewDataDictionary)TempData["ViewData"];
}
How can I then convert this ViewData, which includes the previous invalid ModelState, into a new model to send to the view so that the user sees their invalid input with validation warnings? Oddly enough, if I pass a new instance of my ViewModel retrieved from the database (with the original valid data) to View (), this is ignored and the (invalid) data is displayed in ViewData!
thank
source
share