How do I validate a RadioButton on MVC?
- by user54197
I am using a client side validation and it is starting to get messy, considering I am making a form. With all the textbox and radio button validations, the controller will be overwhelmed. How do I validate and display the error Message for Radio Buttons and multiple textboxes in MVC on the MODEL side?
A simplified version of what I have.
MODEL...
public class ModelData
{
public string ContactName { get; set; }
public string ContactAddress { get; set; }
public string ContactPhone { get; set; }
public string RadioPoliceFire { get; set; }
public string RadioComplaint { get; set; }
//The following is a Failure :(
public string RadioType
{
if (RadioType == null)
{return "Type Required";}
return null;
}
//End Failure
}
CONTROLLER...
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Info(ModelData InfoData)
{
if (infoData.RadioType == null)
{ModelState.AddModelError("RadioType", "Type Required");}
try
{ ...
return RedirectToAction("Confirmation");
catch
{ModelState.AddModelError("RadioComplaint", "Error");}
}