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