Заполнение бритвенного выпадающего списка из списка <объект> в MVC
У меня есть модель:
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"]);
Roles.Add(ur);
}
return Roles;
}
}
А вот контроллер, который загружает представление:
//
// GET: /Admin/AddNewUser
public ActionResult AddNewUser()
{
DbUserRoles Roles = new DbUserRoles();
return View(Roles.GetRoles());
}
Я могу получить элементы в списке для отображения с помощью@foreach
цикл как показано ниже:
@foreach (var item in Model)
{
<tr>
<td>
@item.UserRoleId
</td>
<td>
@item.UserRole
</td>
</tr>
}
Но как мне заполнить выпадающий список с моделью, через которую я прошел, я попытался
@Html.DropDownListFor(x => x.UserRole)
но мне не повезло.