views:

629

answers:

4

I'm trying to go through the NerdDinner example chapter from the ASP.Net MVC 1.0 and I've come across an error. Everything was hunky dory until I got to the part where I need to edit a dinner. I've followed the guide word for word from the creation of the project until this point (at least the best I can tell). However, when I call the SubmitChanges method on the NerdDinnerDataContext object I get an exception that says:

Rule violations prevent saving

I don't notice any differences between my code right now and the code that is in the final project (other than some additional functionality that I haven't added yet, obviously). Basically, I have no idea how to go about troubleshooting this error at this point. I've tried to look for some answers online, with no luck.

Here are some code snippets from my project, though I'm not sure how much good they will be.

from my DinnerRepository class:

    private NerdDinnerDataContext db = new NerdDinnerDataContext();
...
    public void Save()
    {
        db.SubmitChanges();
    }

from the DinnersController

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection formValues)
    {
        // Retrieve existing dinner
        Dinner dinner = dinnerRepository.GetDinner(id);

        // Update dinner with form posted values
        dinner.Title = Request.Form["Title"];
        dinner.Description = Request.Form["Description"];
        dinner.EventDate = DateTime.Parse(Request.Form["EventDate"]);
        dinner.Address = Request.Form["Address"];
        dinner.Country = Request.Form["Country"];
        dinner.ContactPhone = Request.Form["ContactPhone"];

        // Persist changes back to database
        dinnerRepository.Save();

        // Perform HTTP redirect to details page for the saved Dinner
        return RedirectToAction("Details", new { id = dinner.DinnerID });
    }


How can I go about troubleshooting this issue? How can I find what these "rule violations" are?

This is my first SO question, so my apologies if it isn't that great.

+6  A: 

RuleViolations is how Scott Hanselman, the creator of NerdDinners, decided to encapsulate business logic.

He partialed out the Linq To SQL classes and added a function named GetRuleViolations(), which is where he added all of his business rules. Take a look at that method to see what's going on.

Giovanni Galbo
Bingo! I had an invalid phone number in the field, and it was being caught by the GetRuleViolations method in Dinner.cs. Thanks!
atcrawford
No problem, and welcome to stack overflow!
Giovanni Galbo
jason baisden
A: 

For more information on the set up of nerd dinner, be sure to look at the ASP.NET MVC 1.0 book and the free first chapter.

There is a link to it on website you mentioned. Here it is again, http://tinyurl.com/aspnetmvc

BenMaddox
+2  A: 

I had the same problem like atcrawford, but thanx to Giovanni i managed to resolve it. First, when i began this tutorial i populated the phone number from the database with some random numbers. Now when i tried to edit my existing data i received this "rule violations" because the phone number hadn't the correct form. So look in Models folder at Dinner.cs at:

public IEnumerable<RuleViolation> GetRuleViolations()
    {//if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
        //    yield return new RuleViolation("Phone# does not match country", "ContactPhone");

    }

You can see that i commented that line, so the IsValidNumber method on PhoneValidator class is never called.

Or, you can enter the data for the phone number according to the regular expressions from PhoneValidator

A: 

Had the same issue. Phone Number was listed as 000.000.0000 instead of 000-000-0000

Zeri2Cool