我正在尝试在webgrid中显示下拉列表,但我无法让它工作:(
我收到以下错误:(在视图的最后一列)
最佳重载方法匹配 'System.Web.Helpers.WebGrid.Column(string,string, System.Func,string,bool)'有一些无效 arguments \ Visual Studio 2010 \项目\ FOO \条\视图\管理员\ ManageRoles.cshtml
希望你能理解代码。
模型:
public class UserViewModel
{
[Display(Name = "User name")]
public string Name { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
public string Role { get; set; }
public IEnumerable<SelectListItem> Roles1 { get; set; }
public string Email { get; set; }
}
控制器
public ActionResult ManageRoles()
{
var users = Membership.GetAllUsers().Cast<MembershipUser>().Select(x => new UserViewModel
{
Name = x.UserName,
Email = x.Email,
Roles1 = RolesList,
Role = Roles.GetRolesForUser(x.UserName).FirstOrDefault()
});
ViewBag.Roles = users;
return View(users);
}
public IEnumerable<SelectListItem> RolesList
{
get
{
return Roles.GetAllRoles()
.Select(x => new SelectListItem
{
Value = x,
Text = x
})
.ToList();
}
}
查看:
@model IEnumerable<UserViewModel>
@{
ViewBag.Title = "ManageRoles";
var grid = new WebGrid(source: Model, defaultSort: "Name");
}
<h2>ManageRoles</h2>
@grid.GetHtml(
columns: grid.Columns(
grid.Column("Name"),
grid.Column("Email"),
grid.Column("ListItemId", "Role", format: ((item) => Html.DropDownListFor(item.Role, item.Roles1)))
)
)