ModelState.IsValid
always false because I use a dropdownlist in the form that I want to submit and I get this exception:
The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types
Model:
public class NewEmployeeModel
{
[Required(ErrorMessage = "*")]
[Display(Name = "Blood Group")]
public IEnumerable<SelectListItem> BloodGroup { get; set; }
}
View:
<div class="form-group">
@Html.LabelFor(m => m.BloodGroup, new { @class = "control-label col-md-3" })
<div class="col-md-4">
@Html.DropDownListFor(m => m.BloodGroup, Model.BloodGroup, "Please Select", new { @class = "form-control" })
</div>
</div>
Controller:
[HttpPost]
public ActionResult Employee(NewEmployeeModel model)
{
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
if (!ModelState.IsValid)
{
ModelState.AddModelError("Employee", "Model is Not Valid");
return View("Employee", model);
}
else
{
return null;
}
}