Skip to main content
 首页 » 编程设计

c#之如何使用 DropDownList 绑定(bind)到 WebGrid razor asp net mvc 4 中的模型

2024年08月06日36emanlee

在 google 中搜索了几个小时并尝试了不同的语法后,我决定发布这个。问题是我无法获得在 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; } 
} 

Controller :

 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(); 
        } 
    } 

和 View :

@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 没有名为 DropDownList 的适用方法,但似乎有一个名为该名称的扩展方法。无法动态调度扩展方法”

但我已经阅读了很多有关该错误的信息,但我不明白为什么我一直收到它。毕竟我已经将 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

那么“没有扩展方法语法”怎么调用它呢?

请您参考如下方法:

尝试以下格式:

grid.Column( 
    "TodosLosRoles",  
    "Roles",  
    format: item => Html.DropDownList(((int)item.UserId).ToString(), Model[0].TodosLosRoles) 
) 

有点废话,但 WebGrid 总体上是废话。它依赖于弱动态类型,如果你依赖这个组件,你可以忘记强类型和适当的 View 模型。如果您对所传递的类型不是特别小心,请准备好拼命地转换和潜在的运行时错误。