views:

211

answers:

2

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.

A: 

In your model, do you have a check for that condition?

public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(title.Trim()))
                yield return new RuleViolation("Dinner Title is required", "title");

            yield break;
        }

EDIT: Looks like the violations were never being added, just counted. Try this:

if (ModelState.IsValid)
            {

                try
                {
                    ...
                }
                catch
                {
                    ModelState.AddModelErrors(dinner.GetRuleViolations());
                }
            }
            else
            {
                ModelState.AddModelErrors(dinner.GetRuleViolations());
            }
RememberME
Yes I do:public IEnumerable<RuleViolation> GetRuleViolations(){ if (String.IsNullOrEmpty(Title)) yield return new RuleViolation("Title required", "Title"); ... yield break; }
Mark
Try setting your db to not allow nulls for title and see what happens then. When I run NerdDinner, I get a similar issue.
RememberME
Edited my response. See code above.
RememberME
A: 

Finally found the Wrox forum for this book that has the answer. The answer is actually surprising. It has something to do with the debugger in Visual Studio 2010.

Essentially just hit F5 to continue and everything works fine.

Here is the link to the forum thread with more answers:

http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html#post261814

Edward Burns