我正在使用客户端验证,考虑到我正在制作表单,它开始变得混乱。通过所有文本框和单选按钮验证,控制器将不堪重负。如何验证和显示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");}
}
答案 0 :(得分:1)
我喜欢DataAnnotations validation http://www.asp.net/learn/mvc/tutorial-39-cs.aspx或MVC 2 http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
BTW IsRadioButtonChecked应该是bool。