Regarding your error: "'System.Web.Mvc.ModelStateDictionary' does not contain a definition for 'AddModelErrors' and no extension method 'AddModelErrors' accepting a first argument of type 'System.Web.Mvc.ModelStateDictionary' could be found (are you missing a using directive or an assembly reference?)"
I received the same error and noticed some inconsistencies in the text, unless I'm missing something?
In
Part 5, they have you create this function:
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);
}
}
}
For the rest of the instructions on
Part 5 , they consistently have you call 'AddRuleViolations'.
In
Part 6, the same functions that were calling 'AddRuleViolations' are now calling 'AddModelErrors', such as:
Code:
//
// POST: /Dinners/Edit/5
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection) {
Dinner dinner = dinnerRepository.GetDinner(id);
try {
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id=dinner.DinnerID });
}
catch {
ModelState.AddModelErrors(dinner.GetRuleViolations());
return View(new DinnerFormViewModel(dinner));
}
}
I changed the name of the function 'AddRuleViolations' to 'AddModelErrors', which resolved my problem.
Did I miss something in the text, or is this an outdated version?
Regards,
Karsten