简体   繁体   English

ASP.NET MVC 4.0中非侵入式验证中的奇怪行为

[英]Strange behavior in Unobtrusive validation in ASP.NET MVC 4.0

I am using ASP.NET MVC 4.0 and I am experiencing a strange behavior with the JQuery Unobtrusive Validation. 我正在使用ASP.NET MVC 4.0,并且使用JQuery Unobtrusive Validation遇到奇怪的行为。

I have placed its related scripts into my layout.cshtml and when I open up the view and hit the submit button, the validation messages are coming and up and suddenly the server call is hit. 我将其相关脚本放入了layout.cshtml中,当我打开视图并单击“提交”按钮时,确认消息不断出现,突然服务器调用被击中。 ie the debugger steps into the server code for POST event. 即调试器进入POST事件的服务器代码。

So basically, both the things are happening here. 因此,基本上,这两种情况都在这里发生。 The client side validation as well as the server side event. 客户端验证以及服务器端事件。

I can see the messages firing up yet the execution seems to continue and go to server side. 我可以看到消息触发,但执行似乎仍在继续并转到服务器端。

This is my view code : 这是我的查看代码:

 @using (Html.BeginForm("AddUser", "Registration", FormMethod.Post, new { enctype = "multipart/form-data", id = "frm1" }))
        {
            //some HTML markup
            .
            .
            .
            <div class="col-md-1 divside-left-first">
                <button type="submit" value="Save" id="Btnsave" class="btn ui-state-default medium">&nbsp;Submit&nbsp;</button>
            </div>
        }

I have placed the following scripts into the layout.cshtml file at the bottom and they are rendered properly on the browser. 我已将以下脚本放入底部的layout.cshtml文件 ,它们已在浏览器中正确呈现。 I checked. 我检查了。

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

Apart from that, I haven't used any other scripts with these except the native JQuery.js script. 除此之外,除本地JQuery.js脚本外,我没有使用其他任何脚本。

Things I have already tried : 我已经尝试过的事情:

  • Putting scripts on the top section . 将脚本放在顶部 Didn't work. 没用
  • Changing button type from button to input. 将按钮类型从按钮更改为输入。 Didn't work. 没用
  • Tried putting scripts into section view instead of layout.cshtml. 尝试将脚本而不是layout.cshtml放到剖视图中。 Didn't work. 没用

Any help or suggestion as to what I am doing wrong here or not getting would be appreciated. 对于我在这里做错了或没有得到的任何帮助或建议,我们将不胜感激。

After struggling for almost one day, I finally happened to found out the solution for this in my case . 挣扎了将近一天后,我终于碰巧发现了我的情况下,这种情况的解决方案。 It could be something else for any one, but in my case this seemed to work : 对于任何人来说可能都是别的东西,但是在我看来,这似乎可行:

The issue with with the JQuery version that I was using and the unobtrusive validation JS. 我使用的JQuery版本和不干扰用户的验证JS的问题。 They seemed to be not going well together and I observed an error stating "Unexpected token u" in the browser console whenever a POST event occured. 他们似乎不太顺利,每当发生POST事件时,我就会在浏览器控制台中看到一条错误消息,指出“意外令牌u”

So while exploring that error in depth, I came to know that there was some kind of conflict between those two JS files and you need to use one additional JS file that would help eliminate the differences and make them work perfectly together. 因此,在深入研究该错误时,我知道这两个JS文件之间存在某种冲突,因此您需要使用一个附加的JS文件,这将有助于消除差异并使它们完美地协同工作。 That file is : 该文件是:

jquery.migrate jquery.migrate

You just need to add this JS file reference in your code and make sure that all other JS files are in proper place and then run your code. 您只需要在代码中添加此JS文件引用,并确保所有其他JS文件都位于适当的位置,然后运行代码即可。 It was working as expected after this. 此后,它按预期工作。

Hope this helps. 希望这可以帮助。

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

相关问题 不引人注目的ASP.NET MVC自定义验证 - ASP.NET MVC Custom Validation with Unobtrusive ASP.NET MVC4不显眼的验证本地化 - ASP.NET MVC4 Unobtrusive validation localization Asp.net MVC 3:奇怪的验证 - Asp.net mvc 3: Strange Validation 使用jQuery的ASP.Net 2012不干扰验证 - ASP.Net 2012 Unobtrusive Validation with jQuery asp.net MVC 不显眼的浏览器错误 - Browser error in unobtrusive for asp.net MVC ASP.NET MVC路由奇怪或意外行为 - ASP.NET MVC Routing Strange or Unexpected Behavior 使用fluentvalidation和asp.net mvc LessThanOrEqualTo不触发的不显眼的客户端验证 - unobtrusive client validation using fluentvalidation and asp.net mvc LessThanOrEqualTo not firing 视图模型无法在服务器端进行验证后,未调用ASP.NET MVC 3非侵入式验证onError - ASP.NET MVC 3 unobtrusive validation onError not called after view model fails to validate on server side 在 ASP.NET Core MVC 中不工作的 bool 字段的不显眼的客户端验证 - Unobtrusive client-side validation for bool field not working in ASP.NET Core MVC 在 ASP.NET MVC 5 中出现以下错误:不显眼的客户端验证规则中的验证类型名称必须是唯一的。 所需 - Getting the following error in ASP.NET MVC 5: Validation type names in unobtrusive client validation rules must be unique. The […] required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM