2012-12-20 77 views
1

我目前正在嘗試驗證付款單上的條款和條件複選框,以便在選中條款和條件複選框之前不會提交表單。MVC 4在不使用JQuery的情況下驗證複選框

我創建的特定表單只會被一組使用IE6的客戶端使用(因此我無法使用JQuery進行驗證)。

我已經裝修的條款和條件布爾模型值與所需的數據標註但不顯示所需的錯誤信息(不像使用任何字符串,小數或INT等

我設法讓其他值顯示錯誤使用此代碼:

div style="color: red;font-weight:bold;">@TempData["message"]</div> 

if (ModelState.IsValid && model.TermsAndConditions) 
      { 
       ConnectAndRedirectoToProtx(model); 
      } 
      else 
      { 
       //store locally and sort later 
       TempData["message"] = "You must first read through and agree to the terms and conditions"; 
      } 

這個代碼在控制器然後我在視圖這樣渲染的HttpPost指數的ActionResult發現

使用此cpde的問題是它只顯示如果所有其他字段都是正確的,是否有可能不使用JQuery來顯示此錯誤消息以及在提交按鈕被點擊後立即顯示的其他錯誤消息?

+2

還有使用IE6的人??? –

回答

1

所需的數據註釋標記在您的情況下不起作用,因爲即使未選中的複選框也會將假值傳遞給模型。 如果不支持JQuery,可以使用Java Script(直到這也被瀏覽器阻止)。 訪問以下鏈接獲取幫助。

http://www.w3schools.com/js/js_form_validation.asp

而且你可以改變你的控制器功能像下面跟蹤誤差僅爲這種情況下

if(!model.TermsAndConditions) 
{ 
    TempData["message"] = "You must first read through and agree to the terms and conditions"; 
} 

if (ModelState.IsValid) 
     { 
      ConnectAndRedirectoToProtx(model); 
     } 
     else 
     { 
      //store locally and sort later 
      TempData["message"] = "Invalid Model or some other error message......"; 
     }