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 });
}
This is my first SO question, so my apologies if it isn't that great.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With