C# 如何在MVC上验证单选按钮?
我正在使用客户端验证,考虑到我正在制作表单,它开始变得混乱。有了所有的文本框和单选按钮验证,控制器将不知所措。如何在模型侧验证和显示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 {
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时,我会记住这一点。