View Single Post
  #2 (permalink)  
Old July 18th, 2010, 07:52 AM
hjbaard hjbaard is offline
Registered User
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 3 Times in 1 Post

Originally Posted by Darthus View Post
Hey guys,

Noticed some errata in the ViewModel section. I figured I would post it here so other users could see it if they get stuck. Seemed pretty big and had me stuck for a while, so wanted to share.

First, on page 88, it tells you to create a DinnerFormViewModel class, but doesn't say where, the namespace, usings etc. It appeared to be a model, as it uses the Dinner class. So I put it there and made it part of that namespace. I confirmed this is how it is in the NerdDinner 2.0 download zip as well.

However, on page 89 it says to update the inheret tag on your edit page to: Inherits=‚ÄĚSystem.Web.Mvc.ViewPage<NerdDinner.Con trollers.DinnerFormViewModel>

So... I think that's wrong, should be Models, not controllers.

That's a smaller one, the big one that stuck me was Code snippet 1-52 on the bottom of page 89 start of page 90. It tells you to update your view code for your edit page, but aside from changing the SelectFor tag to consume the new Country SelectList, it doesn't have any of the other number of updates you need to do.

Since you're using the ViewModel now, you need to have everything else use m.Dinner.(property). This also needs to be done to the Create page as well of course, (though you combine them in the next section).

Hope this helps someone.
That is not the only thing you have te change.
To make it work you also have to change the POST Edit method in the controller to let the controller save the changes.

The TryUpdateModel(dinner) will not work anymore because the form values it get are Dinner.Tittle in stead of Tittle.
To fix this you have to add a prefix to the TryUpdateModel (overloaded)

Like this:

public ActionResult Edit(int id, FormCollection formValues)

Dinner dinner = dinnerRepository.GetDinner(id);
if (TryUpdateModel(dinner, "Dinner"))
The Following 3 Users Say Thank You to hjbaard For This Useful Post:
athosghost (September 7th, 2010), kpthunder (August 13th, 2010), Pete911 (August 26th, 2010)