I am creating a registration form, there is a field called gender in drop down list. when I select Male or Female from the gender drop down list the value will always be null and return a validation message "Male or Female is not valid for the field"
controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ID, FirstName,DOB,Gender")] AgentMaster agentMaster)
{
if (string.IsNullOrEmpty(Convert.ToString(agentMaster.Gender)))
{
ModelState.AddModelError("Gender", "Gender is required");
}
if (ModelState.IsValid)
{
db.AgentMasters.Add(agentMaster);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(agentMaster);
}
view
@model A.Models.AgentMaster
<div class="form-group">
@Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Gender, new SelectList(new[] { "Male", "Female" }),"--select--")
@Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
</div>
</div>
in model gender is given as
public Nullable<short> Gender { get; set; }
when I debug the code using break points the vale for gender in [httppost] is null, but by giving 1 or 0 it works. so I use validation in controller to convert to string, but it is not working. Iam not getting where I need to change. can anyone please help to find solution ??