I’m using PRG for cleaning up my posts in MVC
On the POST action I store the controller modelstate in tempdata using an action filter and then redirect to the GET method which reloads the modelstate.
GET and POST action headers:
[ImportModelStateFromTempData]
public ActionResult Details(int id) {
var object = load(id);
return View(object);
}
[HttpPost]
[ExportModelStateToTempData]
public ActionResult Details(MyViewModel model) {
update(model); return RedirectToAction("Details", new { id = model.id })
}
if an error occurs I in the post action I add an error to the modelstate which invalidates the modelstate and then redirect to the get action.
My question is how best can I determine, in the GET “Details” action that the model was in an error state after the post? If the modelstate was invalid I want to skip the logic…
var object = load(id);
in the GET action. I’ve tried ModelState.IsValid but this does not work.
You could try something like this:
In your controller you can check the TempData[“IsValid”] field instead of ModelState.IsValid:
Notice how the action filter code is in the OnActionExecuting method. YOu will keep the existing code in the OnActionExecuted: