EDIT: I made something much better to fill and read data from a view using ViewModels, called it ValueInjecter. http://valueinjecter.codeplex.com/
it is used by http://prodinner.codeplex.com - an ASP.net MVC sample application
you can see the best way of using ViewModels in prodinner
using the ViewModel to store the mapping logic was not such a good idea because there was repetition and SRP violation, but now with the ValueInjecter I have clean ViewModels and dry mapping code
That's the old stuff, don't use it:
I made a ViewModel pattern for editing stuff in asp.net mvc
this pattern is useful when you have to make a form for editing an entity and you have to put on the form some drop-downs for the user to choose some values
public class OrganisationBadViewModel
{
//paramterless constructor required, cuz we are gonna get an OrganisationViewModel object from the form in the post save method
public OrganisationViewModel() : this(new Organisation()) {}
public OrganisationViewModel(Organisation o)
{
Organisation = o;
Country = new SelectList(LookupFacade.Country.GetAll(), "ID", "Description", CountryKey);
}
//that's the Type for whom i create the viewmodel
public Organisation Organisation { get; set; }
...
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…