0

I have a gender enum like this: my enumlist.cs

 public enum Gender
{
    [Description("male")]
    male= 0,
    [Description("female")]
    female= 1,
   
}

But when it is displayed in View, I need DropDown to display ---Please Select --- as default value. So that i can check required validation in jQuery Script.

cteate.html ,

   <div class="col-xl-4 col-md-6 col-12">
                    <div class="row">
                        <div class="col-xl-4 col-md-4 col-12 form-title">
                            @Html.LabelFor(model => model.Gender) 
                        </div>
                        <div class="col-xl-8 col-md-8 col-12 form-data">
                          

                        @Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(SubMIS.Models.Gender))), "---Please Select ---", new { @class = "rwd-select" })
                        @Html.ValidationMessageFor(model => model.Gender)
                        <label style="color: red">@ViewBag.GenderError</label>


                        </div>
                    </div>
                </div>

How to do this? what's wrong with my code ?

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
georgetovrea
  • 537
  • 1
  • 8
  • 28

1 Answers1

0

For displaying value from Enum in asp.net has built in helper class asp-items="@Html.GetEnumSelectList<YourEnumName>. However, in your code you are doing otherwise so that you couldn't set a value in that format as setting value using DropDownListFor is not supported. Let's move to the solution.

Enum:

public enum Gender
    {
        [Description("male")]
        male = 0,
        [Description("female")]
        female = 1,
        
    }

View:

 @using DotNetMVCWebApp.Enums
 <select asp-for="Gender" asp-items="@Html.GetEnumSelectList<Gender>()">
   <option selected="selected" value="1">Please select</option>
</select>

Note: I have modified the Enum value as [Description("male")] male = 2, [Description("female")] female = 3, so that I could set default value as 1 as you can see the default value below:

enter image description here

Output:

enter image description here

Work Around To Handle Easily

Modify The Enum:

public enum Gender
    {
        [Display(Name = "Please Select")] Select = 0,
        [Display(Name = "Male")] Male = 1,
        [Display(Name = "Female")] Female = 2,
       
    }

Note: If you can see here I am setting the the default enum as Please Select so that it will be assigned value on DropDownListFor and you do not need to do anything else.

View:

@using DotNet6MVCWebApp.Enums
@Html.DropDownListFor(model => model.Gender, new SelectList(Enum.GetValues(typeof(Gender))), new { @class = "rwd-select" })

Note: Here point to remember just remove "---Please Select ---" from your @Html.DropDownListFor as this will come from the Enum. And only this will required least modification what you are trying to implement.

But I prefer Like below:

@using DotNet6MVCWebApp.Enums
@Html.DropDownList("Gender", Html.GetEnumSelectList<Gender>(),  new { @class = "form-control" })

Output:

enter image description here

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43