c# - MVC ViewModel Class -


i using c# mvc razor.

say if have view shows list, drop down, , 2 text boxes. should of information kept in 1 class pass view?

i can't stress enough importance of using viewmodels passing data controllers view. stated in question, you're doing excellent start!

so .. how it.

public class indexviewmodel // or detailsviewmodel or whatever action method { public ienumerable<foo> foos { get; set; } public selectlist dropdownbox { get; set; } public string name { get; set; } // textbox 1 public string description { get; set; } // textbox 2 } 

ok. lets see i've done.

i've passed information view requires. nothing more, nothing less. -exact- info.

the foos list of foo can loop through render. (pro tip: use displaytemplates render out custom type / collections of custom types).

i've suggested in passing in selectlist drop down contents, etc. people don't (which fine). instead pass in collection of items render in drop down list, find far leaky. key reason why have viewmodels can test these things.

finally, have single property per text box.

so in summary, put information in single viewmodel. view model contain other classes (exposed via properties). yes - want 1 viewmodel per view.

hth :)


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -