Win*_*Win 120 c# html.dropdownlistfor razor asp.net-mvc-4
我有一个模特:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是加载视图的Controller:
//
// GET: /Admin/AddNewUser
public ActionResult AddNewUser()
{
DbUserRoles Roles = new DbUserRoles();
return View(Roles.GetRoles());
}
Run Code Online (Sandbox Code Playgroud)
我可以使用@foreach循环显示列表中的项目,如下所示:
@foreach (var item in Model)
{
<tr>
<td>
@item.UserRoleId
</td>
<td>
@item.UserRole
</td>
</tr>
}
Run Code Online (Sandbox Code Playgroud)
但是我如何使用传递的模型填充下拉列表,我尝试过
@Html.DropDownListFor(x => x.UserRole)
但我没有运气.
Dus*_*gen 238
您可以将业务逻辑分离到视图模型中,这样您的视图就可以更清晰地分离.
首先创建一个viewmodel来存储用户将选择的Id以及将出现在其中的项目列表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; }
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
在控制器内部创建一个方法来获取UserRole列表并将其转换为将在视图中显示的表单.
控制器:
private IEnumerable<SelectListItem> GetRoles()
{
var dbUserRoles = new DbUserRoles();
var roles = dbUserRoles
.GetRoles()
.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);
}
Run Code Online (Sandbox Code Playgroud)
参考文献:
现在创建了视图模型,简化了表示逻辑
视图:
@model UserRoleViewModel
@Html.LabelFor(m => m.SelectedUserRoleId)
@Html.DropDownListFor(m => m.SelectedUserRoleId, Model.UserRoles)
Run Code Online (Sandbox Code Playgroud)
参考文献:
这将产生:
<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>
</select>
Run Code Online (Sandbox Code Playgroud)
Ank*_*ngh 29
@Html.DropDownList("ddl",Model.Select(item => new SelectListItem
{
Value = item.RecordID.ToString(),
Text = item.Name.ToString(),
Selected = "select" == item.RecordID.ToString()
}))
Run Code Online (Sandbox Code Playgroud)
nei*_*ldt 25
一种方式可能是;
<select name="listbox" id="listbox">
@foreach (var item in Model)
{
<option value="@item.UserRoleId">
@item.UserRole
</option>
}
</select>
Run Code Online (Sandbox Code Playgroud)
Jon*_*lis 10
接近的东西:
@Html.DropDownListFor(m => m.UserRole,
new SelectList(Model.Roles, "UserRoleId", "UserRole", Model.Roles.First().UserRoleId),
new { /* any html attributes here */ })
Run Code Online (Sandbox Code Playgroud)
您需要一个SelectList来填充DropDownListFor.对于您需要的任何HTML属性,您可以添加:
new { @class = "DropDown", @id = "dropdownUserRole" }
Run Code Online (Sandbox Code Playgroud)
List<UserRole>您可以让模型包含a 而不是a SelectList<UserRole>.还要添加一个属性SelectedUserRoleId来存储......以及所选的UserRole的Id值.
填写SelectList,然后在View中使用:
@Html.DropDownListFor(x => x.SelectedUserRoleId, x.UserRole)
Run Code Online (Sandbox Code Playgroud)
你应该没事
另请参见http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.108).aspx.
| 归档时间: |
|
| 查看次数: |
324703 次 |
| 最近记录: |