Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.EnumDropDownListFor in Asp.net Core

I'm porting an MVC5 app in Asp.net Core. I cannot find

@Html.EnumDropDownListFor()

was it deprecated and is there a substitute? Is there any documentation where I can find all these little breaking changes?

like image 511
Francesco Cristallo Avatar asked Nov 18 '14 01:11

Francesco Cristallo


1 Answers

For anyone who is still looking for an answer, in ASP.NET 5 the functionality of EnumDropDownListFor() is obtained using DropDownListFor() in combination with GetEnumSelectList() method. For example:

@model Enum
@Html.DropDownListFor(m => m, Html.GetEnumSelectList(Model.GetType()))

Note that you can decorate each value of the Enumeration with custom display names e.g. to include spaces. For instance:

public enum CementTypes {
    [Display(Name = "Class S")]Class_S,
    [Display(Name = "Class N")]Class_N,
    [Display(Name = "Class R")]Class_R 
}
like image 50
Christos K. Avatar answered Sep 20 '22 05:09

Christos K.