I have a partialView that has a <select> that holds a list of available Roles for the user being registered. I'm new to MVC and I'm struggling to figure out how to bind the <select>.
Normally I would do this on the Page_Load of an ascx, for example:
rolesSelect.DataSource = Roles.GetAllRoles().OrderBy(r => r);
rolesSelect.DataBind();
But with MVC it's completely different. My view and partialView look something like this:
Users.cshtml
@model IEnumerable<RobotDog.Models.UserModel>
<table>...</table>
<div id="addUser">
@Html.RenderPartial("_AddUser")
</div>
_AddUser.cshtml
@model RobotDog.Models.RegisterModel
@using(Html.BeginForm("AddUser","Admin", FormMethod.Post)) {
@Html.EditorFor(x => x.Email, new { @class = "input-xlarge", @placeholder = "Email"})
@Html.EditorFor(x => x.UserName, new { @class = "input-xlarge", @placeholder = "User Name"})
@Html.EditorFor(x => x.Password, new { @class = "input-xlarge", @placeholder = "Password"})
@Html.DropDownListFor(????) //not sure how to bind this?
}
My questions are:
- Do I need to pass the appropriate collection from the controller to the view to the partialView or is there a more practical scalable approach?
- Is it possible to have a controller for the partialView so that I would only have to worry about adding the partialView to the view and not the views' controller?
- This really all boils down to what is the standard practice for binding a collection of data to a DropDownList in a PartialView?