2012-12-12 179 views

回答

1

數據註釋還執行客戶端驗證。例如,假設你有用於形狀的模型的FirstName特性如下數據註解:

[Required(ErrorMessage = "Please enter your first name.")] 
public string FirstName { get; set; } 

如果啓用javascript,驗證會出現客戶端,除非用戶輸入表單不會提交在文本框中的東西。如果javascript被禁用,或者如果您錯過了執行客戶端驗證的適當腳本,驗證將在服務器端發生。但是,您必須檢查ModelState是否有效,並在視圖無效時返回視圖:

if (!ModelState.IsValid) 
{ 
    // Do something here if you need to, then return the view 
    return View(); 
} 
相關問題