如何使用 DropDownList 绑定到 WebGrid razor asp net mvc 4 中的模型?
How to use DropDownList binded to model inside WebGrid razor asp net mvc 4?
在 google 中搜索了几个小时并尝试了不同的语法后,我决定 post 这个。问题是我无法获得在 webgrid 中使用 DropDownList 的正确方法。考虑以下模型:
public class UsuariosModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Nombres { get; set; }
public string ApellidoPaterno { get; set; }
public string ApellidoMaterno { get; set; }
public string Titulo { get; set; }
public string Telefono { get; set; }
public string Email { get; set; }
public bool Activo { get; set; }
public string[] Roles { get; set; }
public List<SelectListItem> TodosLosRoles { get; set; }
}
控制者:
public ActionResult UsuariosRoles()
{
List<Models.UsuariosModel> model = new Metodos.Entidades().listaUsuarios();
if (model != null)
{
var roles = (SimpleRoleProvider)Roles.Provider;
var allRoles = roles.GetAllRoles().Select(x => new SelectListItem{
Text = x,
Value = x
}).ToList<SelectListItem>();
foreach (UsuariosModel md in model)
{
md.Roles = roles.GetRolesForUser(md.UserName);
md.TodosLosRoles = allRoles;
}
return View(model);
}
else
{
return new EmptyResult();
}
}
和视图:
@model List<MvcCursosSSP.Models.UsuariosModel>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Roles de los Usuarios -Administración-";
var grid = new WebGrid(source: Model);
}
<h2>Asignar roles a usuarios</h2>
@using (Html.BeginForm()) {
@grid.GetHtml(
columns:grid.Columns(
grid.Column("UserId", "Id de Usuario"),
grid.Column("UserName", "Nombre de Usuario"),
grid.Column("Nombres", "Nombre(s)"),
grid.Column("Titulo", "Título"),
grid.Column("ApellidoPaterno", "Apellido Paterno"),
grid.Column("ApellidoMaterno", "Apellido Materno"),
grid.Column("Telefono", "Teléfono"),
grid.Column("Email", "Email"),
grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, Model[0].TodosLosRoles.Select(u => new SelectListItem{Text= u.Text, Value=u.Value}))),
headerStyle:"headerActivate",
rowStyle:"rowActivate",
selectedRowStyle:"selectedActivate"))
<input type="submit" value="Guardar cambios" />
}
这一行:grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, Model[0].TodosLosRoles.Select(u => new SelectListItem{Text= u.Text, Value=u.Value}))),
我无法摆脱错误:"CS1973: 'System.Web.Mvc.HtmlHelper has no applicable method named DropDownList but appears to have an extension method by that name. Extension Methods cannot be dinamically dispatched"
但我已经阅读了很多有关该错误的信息,但我不明白为什么我一直收到它。毕竟我已经将 DropDownList 格式化为具有 SelectListItems,不是吗。
请帮帮我,我对此很恼火。
更新
我也试过这个:grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, (List<SelectListItem>)item.TodosLosRoles)),
但我一直收到错误,所以有人可以告诉我为什么即使强制转换为原始类型也无法修复它,错误清楚地表明:
Consider casting the dynamic arguments or calling the extension method without the extension method syntax
那我怎么称呼它"without the extension method syntax"?
尝试以下格式:
grid.Column(
"TodosLosRoles",
"Roles",
format: item => Html.DropDownList(((int)item.UserId).ToString(), Model[0].TodosLosRoles)
)
有点废话,但 WebGrid
总的来说是废话。它依赖于弱动态类型,如果你依赖这个组件,你可以忘记强类型和适当的视图模型。如果您对传递的类型不是特别小心,请准备好投掷地狱和潜在的运行时错误。
在 google 中搜索了几个小时并尝试了不同的语法后,我决定 post 这个。问题是我无法获得在 webgrid 中使用 DropDownList 的正确方法。考虑以下模型:
public class UsuariosModel
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Nombres { get; set; }
public string ApellidoPaterno { get; set; }
public string ApellidoMaterno { get; set; }
public string Titulo { get; set; }
public string Telefono { get; set; }
public string Email { get; set; }
public bool Activo { get; set; }
public string[] Roles { get; set; }
public List<SelectListItem> TodosLosRoles { get; set; }
}
控制者:
public ActionResult UsuariosRoles()
{
List<Models.UsuariosModel> model = new Metodos.Entidades().listaUsuarios();
if (model != null)
{
var roles = (SimpleRoleProvider)Roles.Provider;
var allRoles = roles.GetAllRoles().Select(x => new SelectListItem{
Text = x,
Value = x
}).ToList<SelectListItem>();
foreach (UsuariosModel md in model)
{
md.Roles = roles.GetRolesForUser(md.UserName);
md.TodosLosRoles = allRoles;
}
return View(model);
}
else
{
return new EmptyResult();
}
}
和视图:
@model List<MvcCursosSSP.Models.UsuariosModel>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Roles de los Usuarios -Administración-";
var grid = new WebGrid(source: Model);
}
<h2>Asignar roles a usuarios</h2>
@using (Html.BeginForm()) {
@grid.GetHtml(
columns:grid.Columns(
grid.Column("UserId", "Id de Usuario"),
grid.Column("UserName", "Nombre de Usuario"),
grid.Column("Nombres", "Nombre(s)"),
grid.Column("Titulo", "Título"),
grid.Column("ApellidoPaterno", "Apellido Paterno"),
grid.Column("ApellidoMaterno", "Apellido Materno"),
grid.Column("Telefono", "Teléfono"),
grid.Column("Email", "Email"),
grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, Model[0].TodosLosRoles.Select(u => new SelectListItem{Text= u.Text, Value=u.Value}))),
headerStyle:"headerActivate",
rowStyle:"rowActivate",
selectedRowStyle:"selectedActivate"))
<input type="submit" value="Guardar cambios" />
}
这一行:grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, Model[0].TodosLosRoles.Select(u => new SelectListItem{Text= u.Text, Value=u.Value}))),
我无法摆脱错误:"CS1973: 'System.Web.Mvc.HtmlHelper has no applicable method named DropDownList but appears to have an extension method by that name. Extension Methods cannot be dinamically dispatched"
但我已经阅读了很多有关该错误的信息,但我不明白为什么我一直收到它。毕竟我已经将 DropDownList 格式化为具有 SelectListItems,不是吗。
请帮帮我,我对此很恼火。
更新
我也试过这个:grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, (List<SelectListItem>)item.TodosLosRoles)),
但我一直收到错误,所以有人可以告诉我为什么即使强制转换为原始类型也无法修复它,错误清楚地表明:
Consider casting the dynamic arguments or calling the extension method without the extension method syntax
那我怎么称呼它"without the extension method syntax"?
尝试以下格式:
grid.Column(
"TodosLosRoles",
"Roles",
format: item => Html.DropDownList(((int)item.UserId).ToString(), Model[0].TodosLosRoles)
)
有点废话,但 WebGrid
总的来说是废话。它依赖于弱动态类型,如果你依赖这个组件,你可以忘记强类型和适当的视图模型。如果您对传递的类型不是特别小心,请准备好投掷地狱和潜在的运行时错误。