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:
// POST: /Dinners/Create
public ActionResult Create(Dinner dinner) {
if (ModelState.IsValid) {
try {
dinner.HostedBy = "SomeUser";
return RedirectToAction("Details", new {id = dinner.DinnerID });
catch {
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
    if (ModelState.IsValid)
    condition is met.
  2. If the model is not valid the view is returned but a call to
    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?