Reputation: 7613
I have a update controller action which is invoked when the form is posted. In the form there is a radio button that controls the visibility of a drop down field If drop down field is visible and I post the form, database is updated fine and ModelState is valid.
When the drop down field hidden and I post the form, it posts and updates the database. But the ModelState.IsValid = false and the drop down is being reported as Missing value.
How to fix this issue?
[HttpPost]
public ActionResult Update(ResponseModel model)
{
//ModelState.IsValid = false when dropdown field hidden
//validate form fields
//update database
//if we are here, then db update successful
AjaxResponse.AddJsonItem("msg", "Success");
return Result();
}
Upvotes: 1
Views: 3355
Reputation: 35126
It could be that your ResponseModel has a property annotated with some data annotation like [Required] and when you uncheck your radio box that property doesn't get a value.
Post your model and post the values you receive when state is invalid (By checking in debugger).
Since you only have one property and that is integer. Your model state is invalid because integer can not have null value. When you make it hidden the model doesn't have value for the int property. Either replace the dropdown with hidden field on radio box or make it a nullable int.
Upvotes: 2