Using enum for dropdown list in ASP.NET MVC Core

后端 未结 8 1198
你的背包
你的背包 2021-01-31 06:51

I\'m trying to create a dropdown list with an enum property in ASP.NET MVC Core using the tag helper in a Razor view:

Here is the model:

public class Per         


        
8条回答
  •  离开以前
    2021-01-31 07:26

    There needs one case for editing case when there is selected option in DropDownList

    Using enums with the ASP.NET 5 (MVC 6) Select TagHelper

    public enum Gender {
      [Display(Name = "Male")]Male = 1,
      [Display(Name = "Female N")]Female = 2,
      [Display(Name = "Other")]Other = 3 
    }
    

    **For Editing Case:

    @Html.DropDownListFor(m => m, Html.GetEnumSelectList(typeof(Gender)))
    @Html.DropDownListFor(m => m.Gender, Html.GetEnumSelectList()))
    
    @Html.DropDownListFor(m => m.Gender, Html.GetEnumSelectList(), "Select", new { @class = "form-control" })
    

    **For Normal Case:

    
    
    
    
    @Html.DropDownList("Gender", Html.GetEnumSelectList(), "Select", new { @class = "form-control" })
    

提交回复
热议问题