Further explanation of "Using a AddRuleViolations Helper Method" from Nerd Dinner (ASP.NET MVC 2 Tutorial)
Recently I started investigating ASP.NET MVC 2. I do not have any
previous experience programming on the MS stack and, honestly, I was
quite skeptical that I would enjoy any MS tool as much as I love Ruby
on Rails (which I have been using a lot over the past year). To my
surprise, ASP.NET MVC 2 is actually quite nice and I have enjoyed
working through the initial tutorial that I found. The tutorial is
extremely well laid out, informative, and easy to follow especially
since I already know the MVC ideas from Rails. The tutorial is called
Nerd Dinner and can be found here: , called Nerd Dinner and is found
here
of the tutorial that took me a while to figure out. This is more a
reminder/explanation for myself, but I hope that others will find it
useful as well. Issue - Where to place the code?
In step 5 of the tutorial there is a section titled "Using a
AddRuleViolations Helper Method" which did not contain enough detail
for me to implement on my first attempt. Excerpt from step 5:
We can make this code a little cleaner by adding a
"ControllerHelpers" class to the NerdDinner project, and implement an
"AddRuleViolations" extension method within it that adds a helper
method to the ASP.NET MVC ModelStateDictionary class. This extension
method can encapsulate the logic necessary to populate the
ModelStateDictionary with a list of RuleViolation errors:
The basic idea is for all of the RuleViolations to be detected and
stored only in the Model. The Model knows about the business logic,
detects violations, and stores them. The controller pulls the list of
violations directly from the Model and passes the list to the View.
The View blindly renders the violations from the list by displaying their title and
error message. We can change/add/delete our rules at any time without
changing a single line of code in the View or Controller.
Sounds awesome.Problem is, I could not understand where to create this
Helper method in the project and get everything to work nicely.
My google-fu was weak on this one and after reading over many
semi-related blogs posts/stackoverflow threads I realized that I
should just look at the code for the final product. Nerd Dinner has
all of the production code available here. It
was easy to find the solution and understand how everything worked. I
will outline everything below:
2) Create a Helpers directory at the same level as the Models directory.
3) Create a ControllersHelpers.cs class in Helpers directory and place the code from
step 5 in the tutorial in there. Note: Make sure that you include 'using
NerdDinner.Models' inside your ControllersHelpers class so that it
knows about the RuleViolations class definition. I also had to throw
in a 'using System.Web.Mvc' so the ModelStateDictionary could be
found.
publicstaticclassControllerHelpers {03.04.publicstaticvoidAddRuleViolations(thisModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {05.06.foreach(RuleViolation issueinerrors) {07.modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);08.}09.}10.}
4) Add 'using NerdDinner.Helpers' to DinnersController so that the
controller knows where to find the definition of our new
AddRuleViolations() function
5) test (celebrate if it works, cry if it doesn't)
Okay, that's it. Typing this out helped me cement my understanding and
I am sure that I will be back to check how I did this down the road at
some point. I hope you found it useful.