简体   繁体   English

如何通过向导使用asp.net MVC 3不干扰JavaScript验证?

[英]How to use asp.net mvc 3 unobtrusive javascript validation with a wizard?

I am using asp.net mvc 3 and want to make use of unobtrusive javascript . 我正在使用asp.net mvc 3,并希望使用不引人注目的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. 我决定使用jquery表单向导将其分解为一个看上去不应该让人觉得不知所措的向导。 I am not wondering how do I use the data annotations and unobtrusive java-script with wizard. 我不奇怪我该如何在向导中使用数据注释和简洁的Java脚本。

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. 表单末尾只有一个提交按钮,单击该按钮将序列化表单数据并使用该数据对服务器进行ajax发布。

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. 从我通常第一次看到的使用mvc验证的情况来看,用户需要在任何客户端验证被触发之前单击“提交”按钮。 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. 我知道使用带有验证功能的jquery表单插件的jquery向导,但我宁愿尝试尽可能使用一种方法来保持代码一致。

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. 看来该向导使用了jquery validate,但由于某种原因它无法使用mvc jquery validate方式。

With more investigation I found that the wizard needs to see "required" as a class to trigger 经过更多调查,我发现向导需要将“ 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">

I don't see in the documentation for jquery validate them ever using the class to validate the form. 我没有在jquery的文档中看到使用类来验证表单来验证它们。 Is this something the person made up? 这是人组成的吗?

You shouldn't need any extra javascript code on your submit button when using the jquery form wizard (except of course it initialization). 使用jquery表单向导时,您不需要在提交按钮上添加任何其他javascript代码(当然,它需要初始化)。

The unobstrusive part of Microsoft MVC3 just do a boring work of transform all "data-*" attributes marked on your html input (eg data-val-required="blah blah blah") to a well-known rules of jquery-validation. Microsoft MVC3的不引人注目的部分只是将html输入上标记的所有“ data- *”属性(例如data-val-required =“ blah blah blah”)转换为众所周知的jquery验证规则而进行的无聊工作。 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. 因此,您的模型上的RequiredValidationAttribute和朋友(您的自定义验证等)足以启动。

Try this (if you already didn't): 试试这个(如果您还没有的话):

  1. Take your full-form, and slice it on "divs" with a class step , where each div is a step (I think this part you already did). 采取完整的形式,并使用class step将其切成“ div”,其中每个div都是一个步骤(我认为您已经完成了这一部分)。
  2. Your submit and back button must be: 您的提交和后退按钮必须为:

    input class="navigation_button" id="back" type="reset" value="Back" 输入类别=“ navigation_button” id =“返回” type =“重置” value =“返回”

    input class="navigation_button" id="next" type="submit" value="Next" 输入class =“ navigation_button” id =“下一步” type =“提交” value =“下一步”

  3. Initialize your formwizard 初始化你的formwizard

     $(document).ready(function () { $("form").formwizard({ formPluginEnabled: true, validationEnabled: true, focusFirstInput : true }); }); 

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. 希望对您有所帮助,如果对您的问题有误,请对不起。

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. 我通过公司的高流量在100K +网站开发上完成了这些工作。

We use c# MVC, c# razor matrix-style, node.js, php and python so far. 到目前为止,我们使用c#MVC,c#razor矩阵样式,node.js,php和python。 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. 我们的方法非常类似于MVC,它非常严格地分隔HTML,CSS和JavaScript。

It goes like so: 就像这样:

  1. We create a single page that only contains markup and links to CSS and scripts. 我们创建一个页面,仅包含标记以及指向CSS和脚本的链接。 We never place inline styles or inline script other than data attribute configuration within the form elements that are HTML5 compatible and HTML5 form compatible etc. We use JavaScript that conforms to HTLM 5 standards for form validation even if not supported. 除了数据属性配置外,我们不会在HTML5兼容和HTML5表单兼容的表单元素中放置内联样式或内联脚本。即使不支持,我们也使用符合HTLM 5标准的JavaScript进行表单验证。 We hve our own libraries but you can use jQuery tools or similar. 我们拥有自己的库,但您可以使用jQuery工具或类似工具。
  2. You have a main.css file and a main.js file that will run the overall reusable style and logic. 您有一个main.css文件和一个main.js文件,它们将运行整体可重用的样式和逻辑。 Think of these as the composers managing the entire flow. 将这些视为作曲家管理整个流程。
  3. For each step in the wizard, create separate css and javascript that is specific for each step. 对于向导中的每个步骤,请为每个步骤创建单独的CSS和javascript。
  4. namespace your steps in your css so that its very specific. 在CSS中为您的步骤命名空间,使其非常具体。
  5. Use jQuery to run your validation and namespace the selectors similar to how you use the CSS approach so you attach events specifically to where they should reside. 使用jQuery来运行验证和选择器的命名空间,类似于使用CSS方法的方式,因此可以将事件专门附加到它们应驻留的位置。

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. 在您的情况下,您可以更进一步,并在MVC中为每个步骤创建局部视图,并将它们分隔开来,因为它们可以为各自的步骤加载自己的CSS和JavaScript。

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 我这样用

function ValidateStep(){
    var validator = $("form").validate(); // obtain validator
    var anyError = false;
    if (!validator.element(this)) { // validate every input element inside this step
       anyError = true;
    }
    if(anyError) return false;
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM