I'm at the "Handling Edit Errors" (page 67) in chapter 1 of the Professional ASP.NET MVC 1.0 book and I'm running into a problem.
The problem is that when I'm editing a dinner and I click save, it's not catching any of the form errors, even though I left the Title blank. Neither UpdateModel(dinner) or dinnerRepository.Save() throws an error.
When I check the db after the save, the Title field is indeed empty. How can that happen without throwing an error?
Any help would be appreciated. Edit code below:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(dinner);
}
}
Found the problem - It was, in fact, a N00B error on my part. I had the following code commented out for some strange reason:
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
No wonder it wasn't catching the errors... Thanks to RememberMe for trying to help out! I appreciate it.