如何通过向导使用 asp.net mvc 3 不引人注目的 javascript 验证?
我正在使用 asp.net mvc 3 并希望使用 不引人注目的 JavaScript 。我正在设计一个相当长的表格,如果不分解的话可能会让人不知所措。
我决定使用 jquery 表单向导 将其分解为一个不应该看起来那么势不可挡的向导。我不知道如何使用数据注释和不显眼的 java 脚本与向导。
现在一切仍然是一个大的形式,但看起来只是通过向导被打破了。表单末尾只有一个提交按钮,单击该按钮将序列化表单数据并使用该数据将 ajax post 发送到服务器。
从我第一次使用 mvc 验证时看到的情况来看,用户需要在任何客户端验证触发之前单击提交按钮。我想知道一旦他们离开该字段是否可以执行此操作,因为一旦用户点击提交并且没有任何反应,就会让用户感到困惑,然后他们必须再次通过整个向导来查找验证错误。
当他们犯错误时立即通知他们会很好。
我知道 jquery 向导使用具有验证功能的 jquery 表单插件,但如果可能的话,我宁愿尝试坚持使用一种方法来保持代码一致。
编辑
这就是我在提交按钮上的内容
$('#frm').live('submit', function (event) {
event.preventDefault();
var $thisFrm = $(this);
ValidatorParse($thisFrm);//jQuery.validator.unobtrusive.parse(selector);
var clientCheck = $($thisFrm).validate().form();
if (clientCheck) {
// ajax post(server side validation will happen.If it does not pass serverside I send it back usually as json).
}
});
所以我发现了一些问题。首先,验证只会在提交时真正触发。如果它被触发并且验证错误出现在第二个向导步骤上,用户将看不到它(除非某些人可以找到找到第一个错误验证错误并将其切换到该步骤的方法)。
我看到的第二个问题是他们必须等到最后才能查看是否存在验证错误。我宁愿尽快让他们知道。
编辑
向导似乎使用了 jquery 验证,但由于某种原因,它无法使用 mvc jquery 验证方式。
经过更多调查,我发现向导需要将“required”视为一个类来触发,
// will trigger validation
<input type="text" value="" name="AnnualFee" id="AnnualFee"
data-val-required="Annual Fee Required"
data-val="true"
class="required ui-wizard-content ui-helper-reset ui-state-default error">
// will not trigger validation
<input type="text" value="" name="AnnualFee" id="AnnualFee"
data-val-required="Annual Fee Required"
data-val="true"
class="ui-wizard-content ui-helper-reset ui-state-default">
我在 jquery 验证文档中没有看到它们曾经使用该类来验证表单。这是人编出来的吗?
I am using asp.net mvc 3 and want to make use of unobtrusive javascript . I am desiging a form that is quite long and probably would be very overwhelming if not broken up.
I decided to use jquery form wizard to break it into a wizard that should not look so overwhelming. I am not wondering how do I use the data annotations and unobtrusive java-script with wizard.
Right now everything is still in one big form but just looks broken up through the wizard. There is only one submit button at the end of the form that when clicked will serialize the form data and do an ajax post to the server with that data.
From what I seen usually for the first time when using the mvc validation the user needs to click the submit button before any of the client side validation gets trigger. I am wondering if it is possible to do it once they leave the field as it going to confuse the user once they hit submit and nothing happens and then they have to go through the entire wizard again to find the validation errors.
It would be nice to inform them right away when they make a mistake.
I know that jquery wizard using the jquery form plugin that has validation but I rather try to stick with one method if possible to keep the code consistent.
Edit
This is what I have on the submit button
$('#frm').live('submit', function (event) {
event.preventDefault();
var $thisFrm = $(this);
ValidatorParse($thisFrm);//jQuery.validator.unobtrusive.parse(selector);
var clientCheck = $($thisFrm).validate().form();
if (clientCheck) {
// ajax post(server side validation will happen.If it does not pass serverside I send it back usually as json).
}
});
So I see a couple problems with this. First the validation only will really get triggered on submit. If it gets triggered and the validation error is on the second wizard step the user won't see it(unless some how can figure out way to find the first wrong validation error and switch them to that step).
The second problem I see is that they have to wait till the end to see if they have validation errors. I rather let them know asap.
Edit
It seems the wizard uses jquery validate but for some reason it is not working with mvc jquery validate way.
With more investigation I found that the wizard needs to see "required" as a class to trigger
// will trigger validation
<input type="text" value="" name="AnnualFee" id="AnnualFee"
data-val-required="Annual Fee Required"
data-val="true"
class="required ui-wizard-content ui-helper-reset ui-state-default error">
// will not trigger validation
<input type="text" value="" name="AnnualFee" id="AnnualFee"
data-val-required="Annual Fee Required"
data-val="true"
class="ui-wizard-content ui-helper-reset ui-state-default">
I don't see in the documentation for jquery validate them ever using the class to validate the form. Is this something the person made up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 jquery 表单向导 时,您的提交按钮上不需要任何额外的 javascript 代码(当然初始化除外)。
Microsoft MVC3 的不显眼部分只是做了一项无聊的工作,将 html 输入上标记的所有“data-*”属性(例如 data-val-required="blah blah blah")转换为良好的- jquery 验证的已知规则。这是在我们考虑验证表单之前完成的!
因此,模型上的RequiredValidationAttribute 和朋友(您的自定义验证等)足以开始。
试试这个(如果你已经没有):
您的提交和后退按钮必须是:
输入类=“navigation_button”id=“返回”类型=“重置”值=“返回”
input class="navigation_button" id="next" type="submit" value="Next"
初始化表单向导
表单向导将负责调用向导的每个步骤的验证。
在写这篇文章之前,我给自己制作了一个示例表单来证明它是有效的。
希望它有帮助,或者如果我弄错了你的问题,抱歉。
You shouldn't need any extra javascript code on your submit button when using the jquery form wizard (except of course it initialization).
The unobstrusive part of Microsoft MVC3 just do a boring work of transform all "data-*" attributes marked on your html input (e.g. data-val-required="blah blah blah") to a well-known rules of jquery-validation. This is done before we think to validate a form!
So, the RequiredValidationAttribute and friends (your custom validations, etc) on your Models are enough to start.
Try this (if you already didn't):
Your submit and back button must be:
input class="navigation_button" id="back" type="reset" value="Back"
input class="navigation_button" id="next" type="submit" value="Next"
Initialize your formwizard
The formwizard will take care of call the validation for each step of your wizard.
Before writing this, I made myself an example form to prove that it works.
Hope it helps, or sorry if I got your problem wrong.
我和我的团队经常创建向导,并拥有独立的解决方案,不依赖于服务器端的特定技术方法。一些“向导”是结帐流程,其中您有一个页面,其中包含许多结帐步骤。我已经通过我的公司完成了超过 100K 的高流量网站开发工作。
到目前为止,我们使用 c# MVC、c# razor 矩阵样式、node.js、php 和 python。我们采用通用方法,并且可以在所有技术中重复使用它。我们的方法类似于 MVC,它非常严格地分离 HTML、CSS 和 JavaScript。
它是这样的:
因此,您最终会得到适用于任何技术的非常清晰的关注点分离。在您的情况下,您可以更进一步,在 MVC 中为每个步骤创建部分视图,并以这种方式对它们进行划分,因为它们可以为各自的步骤加载自己的 CSS 和 JavaScript。
在我的个人资料中,您可以在结账时看到使用此方法的网站。
我实际上并不认为这是一个完整的答案,需要更多的反馈来充实它。
My team and I routinely create wizards and have an independent solution in terms of it not relying on a specific technology approach on the server-side. Some "wizards" are checkout flows where you have a single page that has many steps in terms of checking out. I've done these on 100K + website development efforts with high traffic through my company.
We use c# MVC, c# razor matrix-style, node.js, php and python so far. We take a general approach and we can reuse this across out technologies. Our approach is MVC-like in the way it very strictly separates HTML, CSS and JavaScript.
It goes like so:
So you end up with a very clean separation of concerns that works for any technology. In your case, you can take it further and create partial views in MVC for each step and compartmentalize them that way as they can load in their own CSS and JavaScript for their respective step.
In my profile you can see websites using this methodology in the checkout.
I don't actually consider this a complete answer and would need more feedback to flesh it out from you.
我这样用的
I used it like this