1

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:

  1. 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?
  2. 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?
  3. This really all boils down to what is the standard practice for binding a collection of data to a DropDownList in a PartialView?
2
  • This seems to be implying that users will only ever be in one role, is that correct? Commented Aug 31, 2012 at 2:33
  • I simplified the question. In reality it will be a ListBox Commented Sep 1, 2012 at 2:41

1 Answer 1

2

Add the Roles collection to the model, and construct the select list as needed.

@Html.DropDownListFor(x => x.Role, 
    Model.Roles.Select(role => 
        new SelectListItem() { Text = role.Name, Value = role.Value }
    )
)

An alternative to adding Roles to the model is to create an HTML Helper method. It's an extension method, so add it like this:

namespace ExtensionMethods
{
    public static class HtmlHelperExtensions
    {
        public static IEnumerable<SelectListItem> GetRoles(this HtmlHelper helper)
        {
            return new[] {
                new SelectListItem() { Text="Role1" },
                new SelectListItem() { Text="Role2" },
            };
        }
    }
}

Then register the namespace in the Web.Config under the Views folder:

<system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="ExtensionMethods"/>
      </namespaces>
    </pages>
</system.web.webPages.razor>

Now you can create the drop-down list:

@Html.DropDownListFor(x => x.Role, Html.GetRoles())
Sign up to request clarification or add additional context in comments.

4 Comments

Dumb question - how to add the Roles collection to the model? Thanks dbaseman
@bflemi3 I was just thinking add a property to the model class, and initialize it in the controller. But whatever works for you. (could also put it in the ViewBag, or make an HTML helper).
Sorry, it's late, I'm not thinking straight. I can't have a public Roles Roles { get; } property. How would I add it to the model?
At first sight, I liked the extension method solution, but while implementing it, I realised that I need to obtain data for the actual row data. From a separation of concerns point of view, I am not sure if I want to add this code to an html extension method. Any views on this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.