Values of all fields set to Action correctly except selected value of Gender dropdown list.
[HttpGet]
public ActionResult AddUser(int roleId)
{ .....
List<Gender> genderList = new List<Gender>
{
new Gender {Name = "Male", Value = "M"},
new Gender {Name = "Female", Value = "F"}
};
ViewBag.Genders = genderList;
return View();
}
View
@model Tuple<UserSearch, UserCreateBindModel, RegisterNewPatient>
@using (Html.BeginForm("CreateNewPatient", "Companies", new { @class="form-inline", Area = "" }, FormMethod.Post))
{
Html.EnableClientValidation();
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.AntiForgeryToken()
@Html.LabelFor(m => m.Item3.FirstName)
</div>
@Html.TextBoxFor(m => m.Item3.FirstName, new { Name = "FirstName", @class = "form-control input-lg", @id="firstName", placeholder = "First Name" })
@Html.LabelFor(m => m.Item3.LastName)
</div>
@Html.TextBoxFor(m => m.Item3.LastName, new { Name = "LastName", @class = "form-control input-lg", @id = "lastName", placeholder = "Last Name" })
<div class="">@Html.LabelFor(m => m.Item3.State)</div>
@Html.DropDownList(
"State",
new SelectList(ViewBag.AllStates,
"Id",
"Name", 0),
"",
new {@class = "form-control input-lg dropdown", style= "width:35%;" })
</div>
// and many others fields
This is issue
@Html.LabelFor(m => m.Item3.Gender)
</div>
@Html.DropDownListFor(m => m.Item3.Gender, new SelectList(ViewBag.Genders,
"Value",
"Name", userData.Gender),
"",
new { @class = "form-control input-lg" })
@Html.ValidationMessageFor(m => m.Item3.Gender,
"", new {@class = "text-danger"})
It always NULL in Action. I tried Html.DropDownList("Gender".... - it's work, I get selected Value of Gender but stop working Required validation.
I Model
public class RegisterNewPatient
{ .......
[Required(ErrorMessage = "The Gender is required")]
public string Gender { get; set; }
}
public class Gender
{
public string Value { get; set; }
public string Name { get; set; }
}
[HttpPost]
public async Task<ActionResult> CreateNewPatient(RegisterNewPatient patient)
{......}
I also tried Bind in Action
CreateNewPatient([Bind(Prefix="Item3")]RegisterNewPatient patient)
And it's work. I get value of selected Gender but stop working all other - all fields null except Gender.
What I'm doing wrong?