如何有条件地禁用某些字段的客户端验证?为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