0

I have edit bar for gender and i want to valid it (Male or Female), there is my model class:

        public int CustomerID { get; set; }
        public int BranchID { get; set; }

        public string FirstName { get; set; }

        public string SirName { get; set; }

        public int Age { get; set; }

        public string Gender  { get; set; }

and here is my view part i want to valid:

       <div class="editor-label">
        @Html.LabelFor(model => model.Gender)
       </div>
       <div class="editor-field">
           @Html.EditorFor(model => model.Gender)
           @Html.ValidationMessageFor(model => model.Gender)
       </div>
2
  • 1
    First of all Gender should be RadioButton Group, for that use Enum. And then make it Required, thats it. Commented Jun 28, 2015 at 7:35
  • i search for it but can't find, can you show me the code ? Commented Jun 28, 2015 at 7:36

1 Answer 1

2

Add an enum

public enum Gender
{
  Male,
  Female,
}

and in model change the property to

public Gender Gender { get; set; }

and in the view use radio buttons for the property

@Html.RadioButtonFor(m => m.Gender, "Male", new { id = "Gender_Male" })
<label for="Gender_Male">Male</label>
@Html.RadioButtonFor(m => m.Gender, "Female", new { id = "Gender_Female" })
<label for="Gender_Female">Female</label>
@Html.ValidationMessageFor(model => model.Gender)
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.