Getting started with MVCContrib Filters
What is MVCContrib? MVCContrib is a open source project was designed to add functionality and ease-of-use to the out of the box experience of the ASP.NET MVC Framework. It kicks in where MVC on its own has left off. MVCContrib is hosted on CodePlex at http://mvccontrib.codeplex.com and it also a CodePlex Foundation Project.
In this post Im going to cover a couple of the Action Filters that are included with MVCContrib, ModelStateToTempData and PassParametersDuringRedirect.
ModelStateToTempData
When you have this filter decorating your action method (or at the controller level) you will get a behavior in which that when model state gets created post model binding you might have a state in which you want available to another action method. This happens when you are practicing the Post-Redirect-Get Pattern (PRG).
Lets say you have a CustomerEditModel that coming from a web form. You need a Post Action that takes in a CustomerEditModel and out of the box that DataAnnotations will engage and validate your model. Below is a model with two attributes, Required and DisplayName. Required may or may not be what you think it means and DisplayName gives you an opportunity to give your labels a different name than from the reflected property type name when using the template helpers (go read Brads series now).
Ok back to the story. When you submitted a nice clean valid model and you flow into the ModelState.IsValid block life is good. When you had some validation issues and you flow to the FixErrors RedirectToAction something particular needs to happen. ModelState has some your validation results that you will want to use to display validation hints back to your end users. The problem is that ModelState isnt carried over, without any intervention to the next Action method being called.
Enter the ModelStateToTempData Attribute
Add a [ModelStateToTempData] to the action method that the modelstate is being generated from, lets call it the sender, AND the action method that needs to use it, maybe call it the reciever?
Whats happening on the sending side with these attributes is when a RedirectToRouteResult is returned from an action (in this case were using RedirectToAction) any data that is the ModelState dictionary will be copied into TempData - A.U.T.O.M.A.T.I.C.A.L.L.Y. You dont have to manually do it yourself!

Whats happening on the receiving side with these attributes is when a ViewResultBase is returned from an action (in this case were using View) all the data that was previously put into TempData is copied out of TempData and put back into ModelState
In this case since were in the FixErrors action the errors that we just got back from the [ModelStateToTempData] attribute will be displayed on the user form. Additionally were also getting back the values of the "bad submission" from the end user so that for non-trivial (or really any) forms they dont have to re-enter their data.
PassParametersDuringRedirect
Since we live in a perfect world and our information management is so well done that our users get all the forms filled out perfectly that that always drop into the ModelState.IsValid block every time. Maybe we can just render the view from there ... NO! My post, my rules. Thou shalt not render view from Post Methods. We have to PRG our way out of there. In this case wed do something interesting with some persistence and then well take the user to a You Are Teh Awesome page.
Lets take the same controller action methods but add one more attribute to them, [PassParametersDuringRedirect]
Id like to point out that the difference between the two RedirectToAction call in the CreateCustomer Action Method. The second one, the one that ships with Asp.NET MVC and the first one has the nice expression in there. This is another nugget that comes with MVCContrib. This flavor of the RedirectToAction extension method will take in a an expression and make sure that the destination method will have the correct parameters passed to it.
The new attribute added to the methods, [PassParametersDuringRedirect] is bringing some more pleasant PRG stuff to the controller. After the valid customer has been persisted we want to show the user a Sucess page. In this case we want to show the model (or maybe another model with additional information) thus the method signature as so public ActionResult Success(CustomerEditModel customerEditModel). This is very similar to the [ModelStateToTempData] behaviour.
I hope this information has helped. Just until very recently I didnt know these things were in the MVCContrib project and now I have some code to delete from our current code base.
File under: ActionFilters Asp.Net MVC MVCContrib- Internet Explorer is like
- I Quit My Job And We Are Moving Home To Montana
- LINQPad with Databases
- Stupid Linq Tricks Record Rollup
- Extension Methods Improving the quality of Life
- C4MVC Reason 42 to Join
- Checking for special characters using LINQ
- Asp net MVC Session State Extension Method
- Unit Testing Just Do It
- Blog.Start
Posts
- 07 Jun 2010 Internet Explorer is like
- 06 May 2010 I Quit My Job And We Are Moving Home To Montana
- 17 Apr 2010 LINQPad with Databases
- 01 Apr 2010 Stupid Linq Tricks Record Rollup
- 30 Mar 2010 Getting started with MVCContrib Filters
- 23 Mar 2010 Extension Methods Improving the quality of Life
- 22 Mar 2010 C4MVC Reason 42 to Join
- 13 Mar 2010 Checking for special characters using LINQ
- 09 Mar 2010 Asp net MVC Session State Extension Method
- 08 Mar 2010 Unit Testing Just Do It
- 15 Feb 2010 Blog.Start