asp.net - Including a model within a model, I think? -
i extremely new asp/mvc3/c# undertaking project requires me list users in database drop down list, ideally want filter shows users in roles (i.e. either admin, staff or student).
however problem lies in following, have controller (requests.cs) method following, , allows me go http://server/requests/userlist
:
public actionresult userlist() { // create our view model var users = membership.getallusers(); var model = new studentlistviewmodel { users = users.oftype<membershipuser>().select(x => new selectlistitem { value = x.provideruserkey.tostring(), text = x.username }) }; return view(model); }
a view model looks this:
using system; using system.collections.generic; using system.linq; using system.web; using system.componentmodel.dataannotations; using system.web.mvc; namespace academicregistry.viewmodels { public class studentlistviewmodel { [display(name = "select user")] public string selecteduser { get; set; } public ienumerable<selectlistitem> users { get; set; } } }
and relevant view:
@model academicregistry.viewmodels.studentlistviewmodel @using (html.beginform()) { @html.labelfor(x => x.selecteduser) @html.dropdownlistfor(x => x.selecteduser, model.users) <button type="submit">ok</button> }
all of these work fine (in fact code found post on here) , view displays want, i.e. dropdown list value set userid , text displayed username.
however want displayed within different view, namely edituser, has model associated (@model academicregistry.models.requests):
@model academicregistry.models.requests @{ viewbag.title = "edit"; } <h2>edit</h2> <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) <fieldset> <legend>requests</legend> @html.hiddenfor(model => model.id) <div class="editor-label"> @html.labelfor(model => model.studentid) </div> <div class="editor-field"> *wanting dropdown list show here* </div> <p> <input type="submit" value="save" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div>
i wondering if knew how go having drop down show needs to? dropdown used on quite few pages ideally needs re-usable, , mentioned filtered show users fall in 3 roles have setup.
you need place code gets list of users , creates selectlistitem list seperate method other controllers can access.
you add users property viewmodels displaying list:
public ienumerable<selectlistitem> users { get; set; }
so @ point, have list need display. can either create user control takes list , renders it, or use built-in dropdownlist have:
@html.dropdownlistfor(x => x.selecteduser, model.users)
Comments
Post a Comment