bzzhdg
Last Updated: February 25, 2016
·
4.324K
· iondrimba
178548

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)
Say Thanks
Respond

2 Responses
Add your response

7752
Soldier

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 ·
8638
Dbc682090a661ab2f687792b10784c03

thks buddy.

over 1 year ago ·