views:

122

answers:

1

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 bool RadioPoliceFire { get; set; }
    public bool 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");}
}
+1  A: 

I like DataAnnotations validation http://www.asp.net/learn/mvc/tutorial-39-cs.aspx or MVC 2 http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

BTW IsRadioButtonChecked should be bool.

nubm
This would be PERFECT!!! If I had MVC 2... I will keep this in mind when they update to MVC 2.