禁用 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);
}
How can I conditionally disable client-side validation of some fields ?
Here is the example of what I need to do:
- user writes name and address and submits.
- Both are validated both server and client side. If validation passes e-mail is send to given address and (name, address) pair is stored.
- as a response same page is displayed (I don't want redirect!) this time with confirmation. User can retype their name and submit. In such case e-mail will be resend to the address that corresponds to given name.
It does not work because: client side validation will not pass when user clicks resent button on the second (confirmation screen). It would say that e-mail is required even if there is no field for it. How can I disable javascript validating email before the confirm page gets send to user ?
Example ASP.NET MVC page
<%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" />
<% } %>
<% } %>
In the model both Email
and Name
are required field to enforce client side validation.
Example controller actions
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这只是一个简单的解决方案,我将隐藏第二次确认的电子邮件字段并保留其值。
Just a simple solution for this, I will hide the email field for the second confirmation and preserve its value.
为什么不使用 2 个不同的视图?您不必重定向。我认为你在这里违反了SRP。这一观点有不止一个目的。
这样,第一个视图可以进行客户端验证,而第二个视图则不能选择它并按照自己的意愿行事。
Why don't you use 2 different views? You don't have to redirect. I think you are violating the SRP here. That view is serving more than one purpose.
That way the first view can have client-side validation and the second view can not opt in for it and do as it wishes.
我认为,这要么不是问题,要么是全部问题。因此,您需要禁用该视图的客户端验证并手动检查要验证的控件。 (从我的头顶上掉下来)。
编辑:如果我想进行手动客户端验证,我会使用 jQuery,因为它在此类任务中非常简单。
I think, here it is either none or all issue. So you need to disable client validation for that view and manually check the controls to be validated. (off the top of my head).
EDIT: If I wanted to do manual client-side validation, I would use jQuery, since it is so straight-forward in such tasks.
当“disabled”属性添加到元素时,可以将其从客户端验证中排除。
对 ASP.NET MVC 中禁用的输入控件禁用客户端验证 - Imran Baloch 的博客
When the "disabled" attribute is added to the element, it is possible to exclude it from the client side validation.
Disabling Client Side Validation For Disabled Input Controls In ASP.NET MVC - Imran Baloch's Blog