Question about validation of Nerd Dinner
In the NerdDinner tutorial, step 5 , there is a paragraph in the middle of the path under "Implemented Full Edit Actions":
The best part about our Edit implementation is that neither our Controller class nor our View template needs to know anything about the specific validation or business rules that apply in our Dinner model. We may add additional rules to our model in the future and do not have to make any code changes to our controller or view in order for them to be maintained. This gives us the flexibility to easily change application requirements in the future with minimal code changes.
My question is what rule can be added and in such a way that I do not lose the net division. I see this code:
public static class ControllerHelpers {
public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) {
foreach (RuleViolation issue in errors) {
modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
}
}
better than this code:
catch {
foreach (var issue in dinner.GetRuleViolations()) {
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(dinner);
}
because I don't have specific information about the class / model and you can use it in the application. And I can see how good it is if my error handling is simple as above, but I don't see how I would add something more complex for the new business rules and hoped for an example.
a source to share
First, make sure that the code you are entering is really just pulling errors from the Model and putting them into the View (via ModelState). The flexibility is that any new rules / changes should only concern the model. those. you only need to click on the Dinner.cs code.
One of the things I had to experience in the NerdDinner tutorials was that validation would happen in both the Dinner.cs OnValidate () partial method and DinnersController.cs in the UpdateModel () call. This call copies the text from the screen to the Model. If, for example, it tries to copy text to float, it will update the ModelState and throw an error. Normal check will not be performed.
a source to share