本文介绍了与MVC和jQuery内嵌的客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装一个简单的例子来说明一个jQuery UI对话框内的格式,并希望该表格上启用内嵌的客户端验证

I have setup a simple example to show a form inside a jquery UI dialog and wish to enable inline client side validation on that form

我已经然后加入脚本到我的母版页

I have then added the scripts to my master page

<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/jquery-1.4.3.min.js" )%>"></script>
<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/jquery.validate.min.js" )%>"></script>
<script type="text/javascript" src="<%: Url.Content( "~/_assets/js/MicrosoftMvcJQueryValidation.js" ) %>"></script>

和以后,我通过以下code启用客户端验证

and then I have enabled Client Side Validation through the following code

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm() { %>
<% } %>

然后,我不知道如何启用在线验证作为每个输入的所以当用户从任何一个离开的重点验证发生

客户端验证,似乎只有在我已经做了提交工作。但是,这不是一个客户端验证的属性,从我的服务器code验证...

The client side validation seems to work only after I have done a submit. But that is not a "client side validation" as the attributes get validated from my server code...

任何建议?

推荐答案

最后,我已经通过了解决方案。

Finally I have got through the solution.

首先,我的形式从未绑定到由 MicrosoftMvcJQueryValidation.js 脚本中的code提供的验证回调。这是因为我使用jQuery对话框和形式的对话框内,而包含在母版页脚本。

First of all, my forms were never binded to validation callbacks provided by the code inside the MicrosoftMvcJQueryValidation.js script. This because I am using jQuery dialogs and the form is inside the dialog while the script included in the master page.

我对解决方案的第一次尝试已经修改 MicrosoftMvcJQueryValidation.js 。特别是我已经添加了一个功能 EnableClientSideValidation(),我感动code,这是在 $(文件)。就绪函数如下code样品中

My first attempt toward the solution has been to modify the MicrosoftMvcJQueryValidation.js. In particular I have added a function EnableClientSideValidation() where I moved the code that was in the $(document).ready function as in the following code sample

function EnableClientSideValidation() {
    var allFormOptions = window.mvcClientValidationMetadata;
    if (allFormOptions) {
        while (allFormOptions.length > 0) {
            var thisFormOptions = allFormOptions.pop();
            __MVC_EnableClientValidation(thisFormOptions);
        }
    }
}

$(document).ready(function () {
    EnableClientSideValidation();
});

然后我呼吁,我放在对话框标记code脚本块内相同功能的 $(文件)。就绪()函数

与萤火虫的帮助下我已经把断点 EnableClientSideValidation()函数内部,然后经历当主页面准备好,但不是从只叫事实对话框。这是由于这样的事实,我在&LT我的对话脚本块;形式&GT; ...&LT; /表&GT; 标记等脚本没有工作。

With the help of firebug I have placed a breakpoint inside the EnableClientSideValidation() function and then experienced the fact that was called only when the main page was ready but not from the dialog. This was due to the fact that I had my "dialog" script block inside the <form>...</form> tag and so the script did not worked.

code这样的

<% using (Html.BeginForm()) { %>

    //DIALOG FORM CODE WAS HERE

    <script type="text/javascript">
    $(document).ready(function () {
        EnableClientSideValidation();
    });
    </script>
<% } %>

已改为

<% using (Html.BeginForm()) { %>

    //DIALOG FORM CODE WAS HERE

<% } %>

<script type="text/javascript">
$(document).ready(function () {
    EnableClientSideValidation();
});
</script>

最后,一切都开始工作!我想感谢并的。有些事情还是错过了,但你的答案刺激了我的头。

Finally everything started working! I would like to thanks vandalo and kdawg for helping in finding a solution. There was something still missed but your answers have stimulated my head.

我张贴这对其他可以有同样的问题。

I am posting this for other that can have the same problem.

这篇关于与MVC和jQuery内嵌的客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:49
查看更多