0

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")

1 Answer 1

0

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" class="form-control">
    <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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.