Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First attempt at Linq to Sql in NerdDinner - Rule violations prevent saving

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.

like image 301
atcrawford Avatar asked Dec 22 '22 12:12

atcrawford


2 Answers

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.

like image 81
Giovanni Galbo Avatar answered Dec 27 '22 10:12

Giovanni Galbo


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

like image 29
user89220 Avatar answered Dec 27 '22 10:12

user89220