Home > OS >  Dropdown list in ASP.NET MVC
Dropdown list in ASP.NET MVC

Time:11-13

I'm learning ASP.NET MVC and I need to make a gender drop down list it shows (Male , Female) to the user but the selected gender value saved in the SQL Database is (1 for the male , 2 for the Female)

Thank you.

I tried using the enum in the Model

public int? Gender { get; set; }

 public enum Gender
    {
       Male,Female
    }

and this code in the view

 @Html.DropDownListFor(m => m.Gender,
            new SelectList(Enum.GetValues(typeof(schoolproject1.Models.Gender))),
            "Select Gender")

CodePudding user response:

I hope you are doing well! You can use the following code to find out your user's selection.

Just you should code like this

C# Back-end Model:

public int Gender {get; set;}

MVC View CsHtml

<select name="Gender" >
    <option value="1">Male</option>
    <option value="2">Female</option>
 </select>

Then you must work and save the value while Back-end is working

Data data = new Data();
data.Gender = Gender;
model.Data.Add(data); // Whether Add or Modify
model.SaveChanges();
  • Related