Friday, 15 April 2011

c# - How should i structure my View Model - ASP.NET MVC -



c# - How should i structure my View Model - ASP.NET MVC -

i wanting create survey using asp.net mvc 3.0. questions have radio buttons , check boxes. want know right syntax view side viewmodel. want store options answers in collection. ienumerable collection use? here of viewmodel code.

[displayname("country")] [required(errormessage = "country required.")] public string country { get; set; } public ienumerable<string> countries { get; set; } [displayname("business")] [required(errormessage = "please select business unit.")] public string businessunit { get; set; } public ienumerable<string> businesses { get; set; } public boolean togetfit { get; set; } public boolean tochallengemyself { get; set; } public boolean tobehealthier { get; set; } public boolean challengeother { get; set; } public string otherstring { get; set; } public void build() { var myservice = new surveyservice(new surveyrepository()); name = myservice.getname(); email = myservice.getemail(); }

what best way info viewmodel when build method called? should using ienumerable or strings?

here code on .aspx page.

<li> country live in? <br /> <%= html.radiobuttonfor(model => model.country, "canada", true) %> ecuador<br /> <%= html.radiobuttonfor(model => model.country, "chile", true) %> republic of ghana <br /> <%= html.radiobuttonfor(model => model.country, "italy", true) %> nigeria <br /> <%= html.radiobuttonfor(model => model.country, "germany", true) %> romania </li> <li> business unit work in?<br /> <%= html.radiobuttonfor(model => model.businessunit, "pharmaceuticals", true ) %> pharmaceuticals <br /> <%= html.radiobuttonfor(model => model.businessunit, "mechanics", true) %> vaccines <br /> <%= html.radiobuttonfor(model => model.businessunit, "r&d") %> r&d <br /> <%= html.radiobuttonfor(model => model.businessunit, "distribution", true) %> distribution <br /> </li> <li> why want take part in workout? <br /> <%= html.checkboxfor(model => model.togetfit ) %> in shape <br /> <%= html.checkboxfor(model => model.tochallengemyself ) %> challenge myself <br /> <%= html.checkboxfor(model => model.tobehealthier) %> healthier <br /> <%= html.checkboxfor(model => model.challengeother) %> other <%= html.textboxfor(model => model.otherstring) %> </li>

i new asp.net mvc have experience both asp.net , mvc pattern. want much emphasis placed on seperation of concerns possible.

my controller class calls build method. have service class grabs repository object grabbing info model/database.

! want radio buttons dynamically taken database. , if user has selected previous session want found in viewmodel , selected when load page.

the problem see approach above radio buttons , check boxes defined in view. problem if coming out of database or enum or something. should add together viewmodel property represents possible options given radio/checkbox group, property holds selected values user chose group.

first, check out these great helper methods "checkboxlistfor" , "radiobuttonlistfor". re-create them project.

next, need expose collections on viewmodel ienumerable<selectlistitem>. these possible options radiolist/checkboxlist/dropdown element.

then, viewmodel, need define prop single selected alternative (from dropdown or radio button list) or collection of selected alternative (from checkbox list). can utilize int/string item's database id, or if type selected enum, can bind straight that.

finally, view become super simple, , won't need touched if add together new options. in case of database-driven options, no code @ has change.

below sample demo page have open. believe should able @ , apply own project.

the viewmodel:

public class orderviewmodel { public int favoritemovieid { get; set; } public list<int> movieidsformoviesilike { get; set; } public moviecategories favoritemovietype { get; set; } public list<moviecategories> moviecategoriesilike { get; set; } public ienumerable<selectlistitem> moviestoselectfrom { { homecoming x in orderdetailsrepo.getallmovies() select new selectlistitem { text = x.title, value = x.id.tostring(), selected = movieidsformoviesilike.contains(x.id), }; } } public ienumerable<selectlistitem> moviecategoriestoselectfrom { { homecoming cat in enum.getvalues(typeof(moviecategories)).cast<moviecategories>() select new selectlistitem { text = cat.tostring(), value = cat.tostring(), selected = moviecategoriesilike.contains(cat), }; } } public orderviewmodel() { // ensure never null movieidsformoviesilike = new list<int>(); moviecategoriesilike = new list<moviecategories>(); } }

the domain model class , method provide collections:

public static class orderdetailsrepo { public static list<movie> getallmovies() { homecoming new list<movie> { new film { id = 0, title = "great expectation" }, new film { id = 1, title = "gone wind" }, new film { id = 2, title = "lion of winter" }, }; } } public class film { public string title { get; set; } public int id { get; set; } } public enum moviecategories { horror, drama, comedy, }

and super-simplified view:

@model mvc3app.viewmodels.orderviewmodel @using mvc3app.mvchtmlhelpers @* radiobuttonlist , checkboxlist defined in here *@ @{ viewbag.title = "viewpage1"; } <script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (html.beginform()) { @html.validationsummary(true) <div class="editor-label"> what's favorite movie? </div> <div class="editor-field"> @html.radiobuttonlistfor(model => model.favoritemovieid, model.moviestoselectfrom) @html.validationmessagefor(model => model.favoritemovieid) </div> <div class="editor-label"> movies in general? </div> <div class="editor-field"> @html.checkboxlistfor(model => model.movieidsformoviesilike, model.moviestoselectfrom) @html.validationmessagefor(model => model.movieidsformoviesilike) </div> <div class="editor-label"> what's favorite film genre? </div> <div class="editor-field"> @html.radiobuttonlistfor(model => model.favoritemovietype, model.moviecategoriestoselectfrom) @html.validationmessagefor(model => model.favoritemovietype) </div> <div class="editor-label"> film genres in general? </div> <div class="editor-field"> @html.checkboxlistfor(model => model.moviecategoriesilike, model.moviecategoriestoselectfrom) @html.validationmessagefor(model => model.moviecategoriesilike) </div> <br /><br /> <input type="submit" value="save" /> }

c# asp.net-mvc asp.net-mvc-3 model-view-controller viewmodel

No comments:

Post a Comment