How do I validate a RadioButton on MVC?

Posted by user54197 on Stack Overflow See other posts from Stack Overflow or by user54197
Published on 2010-04-07T14:37:03Z Indexed on 2010/04/07 15:03 UTC
Read the original article Hit count: 845

Filed under:
|
|
|

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");}
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc