如何有條件地禁用某些字段的客戶端驗證?爲ASP.NET MVC中的某些字段禁用客戶端驗證
這裏是什麼,我需要做的例子:
- 用戶寫的名字和地址,並提交。
- 兩者都經過服務器端和客戶端的驗證。如果驗證通過,電子郵件將發送到給定地址,並且(名稱,地址)對被存儲。
- 作爲響應顯示同一頁面(我不想重定向!)這一次與確認。用戶可以重新鍵入他們的名字並提交。在這種情況下,電子郵件將被重新發送到與給定名稱相對應的地址。
它不起作用,因爲:當用戶單擊第二個(確認屏幕)上的重新發送按鈕時,客戶端驗證將不會通過。它會說即使沒有字段,也需要電子郵件。在確認頁面發送給用戶之前,如何禁用javascript驗證電子郵件?
例ASP.NET頁面MVC
<%if (Model.Confirm == false){ %>
<% using (Html.BeginForm()) { %>
Your email: <%: Html.EditorFor(m => m.Email) %>
Your name: <%: Html.EditorFor(m => m.Name) %>
<input type="submit" value="Send Email" />
<% } %>
<%} else{ %>
The e-mail was send! You can write your name and click button to resend it.
<% using (Html.BeginForm()) { %>
Your name: <%: Html.EditorFor(m => m.Name) %>
<input type="submit" value="Resend me email again" />
<% } %>
<% } %>
在該模型中都Email
和Name
需要字段來執行客戶端驗證。
例控制器動作
public ActionResult Index(){
return new MyModel {Confirm = false;}
}
[HttpPost]
public ActionResult Index(MyModel model){
if(DataHelper.isKnown(model.Name)){
//send e-mail to the address corresponding to name in database
}
if(!ModelState.IsValid) {
return View(model);
}
//Send e-mail to address in email
//store pair name->email to database
model.Confirm = true;
return View(model);
}
是的,我是...很棒的筆記,謝謝! – drasto 2011-03-22 09:02:40