Thread: Errata
View Single Post
  #10 (permalink)  
Old October 7th, 2010, 10:20 AM
flyinhawaiian flyinhawaiian is offline
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default m.Title versus m.Dinner.Title

Another way to fix issue on page 89 is to use a Custom-Shaped ViewModel Class (which is mentioned in page 91).

Just modify your existing DinnerFormViewModel.cs to expose individual properties of Dinner:

public string Title
    get { return Dinner.Title; }
    set { Dinner.Title = value; }
If you expose just the properties needed for the view, you can still use
<%: Html.LabelFor(m => m.Title) %> in the view.

Also, since the view never directly references Dinner, you can make it private.

There are times where it is very useful to be able to shape the view like this (remember "separation of concearns"). Look at m.Title versus m.Dinner.Title. Think about this. What would happen if you swapped out the Dinner model with something new like FineDining model? You could then change the viewmodel and not change anything in the view. If needed you could "translate" property names like this:

public string Title
	get { return FineDining.MyFineTitle; }
	set { FineDining.MyFineTitle= value; }
Using this modified ViewModel has apparently fixed issue with page 90. So using "if (TryUpdateModel(dinner))" works now. And doesnt need to be changed to "if (TryUpdateModel(dinner,"Dinner"))"

btw, thanks to all who contributed here. It really helped me out.

Last edited by flyinhawaiian; October 7th, 2010 at 11:01 AM.. Reason: added code tags