Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在MVC上验证单选按钮?_C#_.net_Asp.net_Asp.net Mvc - Fatal编程技术网

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

C# 如何在MVC上验证单选按钮?,c#,.net,asp.net,asp.net-mvc,C#,.net,Asp.net,Asp.net Mvc,我正在使用客户端验证,考虑到我正在制作表单,它开始变得混乱。有了所有的文本框和单选按钮验证,控制器将不知所措。如何在模型侧验证和显示MVC中单选按钮和多个文本框的错误消息 我所拥有的东西的简化版本 模型 public class ModelData { public string ContactName { get; set; } public string ContactAddress { get; set; } public string ContactPhone {

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

我所拥有的东西的简化版本

模型

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
}
控制器

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

我喜欢DataAnnotations验证或MVC2


顺便说一句,IsRadioButtonChecked应该是bool。

这将是完美的!!!如果我有MVC2。。。当他们更新到MVC2时,我会记住这一点。