如何使用DropDownList的绑定到的WebGrid剃刀ASP净MVC 4内部模型? [英] How to use DropDownList binded to model inside WebGrid razor asp net mvc 4?

查看:118
本文介绍了如何使用DropDownList的绑定到的WebGrid剃刀ASP净MVC 4内部模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌中搜索和尝试不同的构造结小时,我决定发布此。问题是我无法用里面的WebGrid一个DropDownList的正确方法。考虑下面的模型:

 公共类UsuariosModel
{
    公众诠释用户ID {搞定;组; }
    公共字符串用户名{获得;组; }
    公共字符串Nombres {搞定;组; }
    公共字符串ApellidoPaterno {搞定;组; }
    公共字符串ApellidoMaterno {搞定;组; }
    公共字符串TITULO {搞定;组; }
    公共字符串Telefono {搞定;组; }
    公共字符串电子邮件{获得;组; }
    公共BOOL ACTIVO {搞定;组; }
    公共字符串[] {角色获得;组; }
    公开名单< SelectListItem> TodosLosRoles {搞定;组; }
}

控制器:

 公众的ActionResult UsuariosRoles()
    {
        清单< Models.UsuariosModel>模型=新Metodos.Entidades()listaUsuarios()。
        如果(型号!= NULL)
        {
            VAR角色=(SimpleRoleProvider)Roles.Provider;
            。VAR allRoles = roles.GetAllRoles()选择(X =>新建SelectListItem {
              文本= X,
              值= X
            })了ToList< SelectListItem>();
            的foreach(模型UsuariosModel MD)
            {
                md.Roles = roles.GetRolesForUser(md.UserName);
                md.TodosLosRoles = allRoles;            }            返回查看(模型);
        }
        其他
        {
            返回新EmptyResult();
        }
    }

和视图:

  @model名单< MvcCursosSSP.Models.UsuariosModel>@ {
    布局=〜/查看/共享/ _Layout.cshtml
    ViewBag.Title =角色德洛斯USUARIOS-Administración-
    VAR电网=新的WebGrid(来源:模型);    }< H2> Asignar角色一个USUARIOS< / H>
@using(Html.BeginForm()){@ grid.GetHtml(
列:grid.Columns(
grid.Column(用户ID,标识德Usuario),
grid.Column(用户名,农布雷迪奥斯Usuario),
grid.Column(Nombres,农布雷(多个)),
grid.Column(TITULO,性标题),
grid.Column(ApellidoPaterno,Apellido帕特诺),
grid.Column(ApellidoMaterno,Apellido Materno),
grid.Column(Telefono,Teléfono),
grid.Column(电子邮件,电子邮件),
grid.Column(TodosLosRoles,角色,格式:(项目)=> Html.DropDownList(item.UserId,型号[0​​] .TodosLosRoles.Select(U =>新建SelectListItem的{text = u.Text,值= u.Value}))),
headerStyle:headerActivate
rowStyle:rowActivate
selectedRowStyle:selectedActivate))<输入类型=提交值=和保存cambios/>
}

在这一行: grid.Column(TodosLosRoles,角色,格式:(项目)=> Html.DropDownList(item.UserId,型号[0​​] .TodosLosRoles.Select( U =>新建SelectListItem的{text = u.Text,值= u.Value})))

我无法摆脱错误的:CS1973:'System.Web.Mvc.HtmlHelper有一个名为DropDownList中没有适用的方法,但似乎有该名称的扩展方法不能dinamically派出的扩展方法。/

但我已经读了很多关于这个错误,我不明白为什么我总是收到它。毕竟我已经格式化的DropDownList有SelectListItems,没有一

请帮我对此很恼火。

更新

我想这也: grid.Column(TodosLosRoles,角色,格式:(项目)=> Html.DropDownList(item.UserId,(名单< SelectListItem>)项目。 TodosLosRoles))

不过,我不断收到错误,因此有人可以告诉我,为什么连铸造原始类型不能解决问题,错误清清楚楚地写着:


  

考虑强制转换动态参数或调用扩展方法没有扩展方法的语法


那么,如何把它称为不带扩展方法的语法?


解决方案

请尝试以下格式:

  grid.Column(
    TodosLosRoles
    角色,
    格式:项目=> Html.DropDownList(((int)的item.UserId)的ToString(),型号[0​​] .TodosLosRoles)

这是有点废话,但的WebGrid 是胡扯一般。它依赖于弱动态类型,你可以对强类型和适当的视图模型,如果你靠这个组件上算了。 prepare投的地狱和潜在的运行时错误,如果你不是非常小心你正在传递类型。

After hours of searching in google and trying different syntaxis I decided to post this. The problem is I can't get the right way to use a DropDownList inside a webgrid. Consider the following model:

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

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

And the 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" />
}

on this line: grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, Model[0].TodosLosRoles.Select(u => new SelectListItem{Text= u.Text, Value=u.Value}))),

I can't get rid of the error: "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"

But I have read a lot about that error and I can't understand why I keep getting it. After all I've formatted the DropDownList to have SelectListItems, haven't I.

Please help I'm rather annoyed by this.

Update

I tried this also: grid.Column("TodosLosRoles", "Roles", format:(item)=>Html.DropDownList(item.UserId, (List<SelectListItem>)item.TodosLosRoles)),

But I keep getting the error, so can someone tell me why even casting to the original type doesn't fix it, the error clearly says:

Consider casting the dynamic arguments or calling the extension method without the extension method syntax

So how do I call it "without the extension method syntax"?

解决方案

Try the following format:

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

It's kinda crap, but WebGrid is crap in general. It relies on weak dynamic typing, you can forget about strong typing and proper view model if you rely on this component. Prepare to cast as hell and for potential runtime errors if you are not extremely careful about the types you are passing.

这篇关于如何使用DropDownList的绑定到的WebGrid剃刀ASP净MVC 4内部模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆