View Single Post
  #8 (permalink)  
Old May 7th, 2009, 07:01 PM
njappboy njappboy is offline
Registered User
 
Join Date: May 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default /Dinners/Create POST error

has anyone else run into this issue (our did I just not read carefully and miss a code change)

on page 89 (chapter 1) the DinnersController implements a Create (POST) method to create/persist a new Dinner object.

The code shown in the book is as follows:
Code:
//
// POST: /Dinners/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Dinner dinner) {
if (ModelState.IsValid) {
try {
dinner.HostedBy = "SomeUser";
dinnerRepository.Add(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new {id = dinner.DinnerID });
}
catch {
ModelState.AddRuleViolations(dinner.GetRuleViolations());
}
}
return View(dinner);
}
I don't see how this method is supposed to ever work properly for a few reasons:
  1. HostedBy has a validation rule (pg 39) that says it can't be null or empty. In this case it will always be null when IsValid is called because the property isn't set until the
    Code:
    if (ModelState.IsValid)
    condition is met.
  2. If the model is not valid the view is returned but a call to
    Code:
    ModelState.AddRuleViolations(dinner.GetRuleViolations());
    is never made so the actual rule violations are not added to the ModelState and consequently no error are displayed on the view.
Did I miss a step outlined in the chapter or is this an accurate assessment of this implementation?