2010-04-07 35 views
2

我正在使用客戶端驗證,並開始變得混亂,考慮到我正在製作一個表單。隨着所有文本框和單選按鈕的驗證,控制器將不堪重負。如何在MODEL側驗證並顯示MVC中單選按鈕和多個文本框的錯誤消息?如何驗證MVC上的RadioButton?

我所擁有的簡化版本。

模型...

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

回答