Convert Enum to MVC Html.DropDownList
@{
IList<SelectListItem> list= Enum.GetValues(typeof(YourEnumType)).Cast<YourEnumType>().Select(x => new SelectListItem { Text = x.ToString(), Value = ((int)x).ToString() }).ToList();
SelectList data = new SelectList(list,"Value","Text");
}
@Html.DropDownList("idControl", data)
Written by Ion D. Filho
Related protips
2 Responses
Yo siempre uso algo así:
/*****Controller*****/
var ValorParaPreAsignar = 2;
var CatalogoAMostrar=db.Tabla.where(x=> x.activo==true).ToList();
ViewBag.MiFuenteDeDatos = new SelectList(CatalogoAMostrar, "IdCatalogo", "Descripcion", ValorParaPreAsignar);
/*****Vista*****/
@Html.DropDownList("MiFuenteDeDatos")
over 1 year ago
·
thks buddy.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Mvc
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#