0

In a pretty old .NET tutorial, "Nerd Dinner", it talks about using a Helper Class for Rule Violations. Everything seems straight forward except I'm not sure where to put this class so I can reference it. I am pretty new at MVC.

All of this below was taken from Nerd Dinner Tutorial:

Using a AddRuleViolations Helper Method

Our initial HTTP-POST Edit implementation used a foreach statement within its catch block to loop over the Dinner object's Rule Violations and add them to the controller's ModelState collection:

catch {
    foreach (var issue in dinner.GetRuleViolations()) {
        ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
    }

    return View(dinner);
}

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:

public static class ControllerHelpers {

public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable errors) {

   foreach (RuleViolation issue in errors) {
       modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
   }

} }

JustJohn
  • 1,362
  • 2
  • 22
  • 44
  • Why are you using a tutorial for MVC 1.0? Current version is 5.0 and I believe (don't do much MVC myself at this point) a lot has changed in the interim. – Tim Oct 06 '15 at 21:10
  • I understand it is an older tutorial. Also, I'm pretty sure my question would be the same: where do I create the "Controller Helpers" class? Do I put it in the Controller that has all my CRUD? or in a class of it's own? or? – JustJohn Oct 07 '15 at 00:07
  • Ok, I give in. I am going to try using the validation stuff in my app which is MVC 5. This stuff has changed so much since 1, you are correct. And it keeps changing, MVC, EF, yada yada. Someone should create checklists of what to look for when making an updgrade or install of one of the 1,000 things an MVC solution is made of. thank you. – JustJohn Oct 15 '15 at 23:41

0 Answers0