Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

c# - How to use DropDownList binded to model inside WebGrid razor asp net mvc 4?

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"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...