如何在MVC上验证单选按钮?

时间:2010-04-07 14:37:04

标签: c# .net asp.net asp.net-mvc

我正在使用客户端验证,考虑到我正在制作表单,它开始变得混乱。通过所有文本框和单选按钮验证,控制器将不堪重负。如何验证和显示MODEL侧的MVC中的单选按钮和多个文本框的错误消息?

我所拥有的简化版本。

... 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 个答案:

答案 0 :(得分:1)