All Visual Studio blogs in one place

AddThis Social Bookmark Button
I've been working on an ASP.NET MVC application in which we frequently apply the Post/Redirect/Get pattern. One of the direct consequences of applying this pattern is that you often want to persist the model state across redirects, so that you don't lose validation errors, or the values of input fields.

To persist the model state across redirects, we can put TempData to work. The sole purpose of TempData is exactly this; persisting state until the next request.
public ActionResult Index() { ViewData.Model = ... if (TempData.ContainsKey("ModelState"))
ModelState.Merge((ModelStateDictionary)TempData["ModelState"]); return View(); } [HttpPost] public ActionResult Update(AddModel inputModel) { if (ModelState.IsValid) ... TempData["ModelState"] = ModelState; return RedirectToAction("Index"); } So this works, but I found it to be a bit too cumbersome. And so did Davy Brion, he introduced a clean abstraction into the project, smoothing out some of the friction: making use of action filter attributes, we were able to eliminate duplication across controllers, leaving behind an AOP-ish taste.

The SetTempDataModelStateAttribute stores the model state...(Read whole news on source site)



Enter your email address:

Delivered by FeedBurner

Increase your website traffic with


Anti-spam: How many eyes has a typical person?

Follow us on FB

Home : Blog List : Jef Claes : Persisting model state when using PRG