views:

24

answers:

0

I'm following the MVC2 NerdDinner tutorial and I can't make heads or tails of the model validation area.

I have an Area class created by my .dbml. Then I created a partial Area class myself and added:

partial void OnValidate(ChangeAction action)
{
    if (!IsValid)
    throw new ApplicationException("Rule violations prevent saving");
}

And in my AreaControllers.cs:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            Area area = areaRepository.GetArea(id);

            try
            {
                UpdateModel(area);
                areaRepository.Save();

                return RedirectToAction("Details", new { id = area.ID });

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

                return View(area);
            }
        }

And now for example, whenever I try to save an Edit where a RuleViolation is set, the throw new ApplicationException fires and freezes the entire application.

The NerdDinner tutorial tells me to write that in but doesn't explain what to do with it or how it helps me.

If I remove the !IsValid condition, the Edit is saved no matter if a form value is null or not.

Any suggestions?

Thank you!