Rather than a quick fix I’m more just wondering what others are doing.
The problem is that I am using ModelState for error/validation reporting to the user, but when I use a RedirectToAction(), the ModelState is lost. To overcome this I’ve added some code to our base controller (that all other controllers inherit), in the override of OnActionExecuting I’ve added:
if (TempData["ModelState"] != null && !ModelState.Equals(TempData["ModelState"]))
ModelState.Merge((ModelStateDictionary)TempData["ModelState"]);
And in the override of OnActionExecuted I’ve added:
TempData["ModelState"] = ModelState;
The problem with this is it means some error messages can be displayed on the wrong pages. I just run a ModelState.clear() at the start of actions I know will want to override/correct the current errors but there are cases where it is never cleared and errors show up in weird places.
How do other people handle this? any ideas welcome. Thanks
I have implemented the solution mentioned here:
http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx
in Point 13 (Use PRG Pattern for Data Modification).
It looks fairly similar to what you are doing but using Action Filters and I haven’t noticed any error messages on the wrong pages as of yet.