Win Win - 3 months ago 31
C# Question

Populating a razor dropdownlist from a List<object> in MVC

I have a model:

public class DbUserRole
public int UserRoleId { get; set; }
public string UserRole { get; set; }

public class DbUserRoles
public List<DbUserRole> GetRoles()
BugnetReports RoleDropDown = new BugnetReports();
List<DbUserRole> Roles = new List<DbUserRole>();
DataSet table = RoleDropDown.userRoleDropDown();
foreach (DataRow item in table.Tables[0].Rows)
DbUserRole ur = new DbUserRole();
ur.UserRole = Convert.ToString(item["UserRoleName"]);
ur.UserRoleId = Convert.ToInt32(item["UserRoleID"]);
return Roles;

And here is the Controller that loads the view:

// GET: /Admin/AddNewUser

public ActionResult AddNewUser()
DbUserRoles Roles = new DbUserRoles();
return View(Roles.GetRoles());

I can get the items in the list to display using a
loop as shown below:

@foreach (var item in Model)

But how do I populate a dropdownlist with the model that is passed through, I have tried

@Html.DropDownListFor(x => x.UserRole)

but I'm having no luck.


You can separate out your business logic into a viewmodel, so your view has cleaner separation.

First create a viewmodel to store the Id the user will select along with a list of items that will appear in the DropDown.


public class UserRoleViewModel
    // Display Attribute will appear in the Html.LabelFor
    [Display(Name = "User Role")]
    public int SelectedUserRoleId { get; set; }
    public IEnumerable<SelectListItem> UserRoles { get; set; }


Inside the controller create a method to get your UserRole list and transform it into the form that will be presented in the view.


private IEnumerable<SelectListItem> GetRoles()
    var dbUserRoles = new DbUserRoles();
    var roles = dbUserRoles
                .Select(x =>
                        new SelectListItem
                                Value = x.UserRoleId.ToString(),
                                Text = x.UserRole

    return new SelectList(roles, "Value", "Text");

public ActionResult AddNewUser()
    var model = new UserRoleViewModel
                        UserRoles = GetRoles()
    return View(model);


Now that the viewmodel is created the presentation logic is simplified


@model UserRoleViewModel

@Html.LabelFor(m => m.SelectedUserRoleId)
@Html.DropDownListFor(m => m.SelectedUserRoleId, Model.UserRoles)


This will produce:

<label for="SelectedUserRoleId">User Role</label>
<select id="SelectedUserRoleId" name="SelectedUserRoleId">
    <option value="1">First Role</option>
    <option value="2">Second Role</option>
    <option value="3">Etc...</option>