Last Updated: September 30, 2021
·
13.29K
· iondrimba

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)

2 Responses
Add your response

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 ·