为什么UnobtrusiveJavaScriptEnabled = true来禁用我的AJAX工作吗?

编程入门 行业动态 更新时间:2024-10-25 10:21:57
本文介绍了为什么UnobtrusiveJavaScriptEnabled = true来禁用我的AJAX工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在做用MVC3剃刀样本,并写这样的:

I was doing a sample using MVC3 razor, and wrote this:

<p> Show me the time in: @Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" }) @Ajax.ActionLink("BST", "GetTime", new { zone = "bst" }, new AjaxOptions { UpdateTargetId = "myResults" }) @Ajax.ActionLink("MDT", "GetTime", new { zone = "mdt" }, new AjaxOptions { UpdateTargetId = "myResults" }) </p> <div id="myResults" style="border: 2px dotted red; padding: .5em;"> Results will appear here </div> <p> This page was generated at @DateTime.UtcNow.ToString("h:MM:ss tt") (UTC) </p>

我所有的Ajax调用没有工作,直到我改变了这种关键在web.config中:

All my ajax calls didn't work until i changed this key in web.config:

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

这是我在这篇文章中读到:weblogs.asp/owscott/archive/2010/11/17/mvc-3-ajax-redirecting-instead-of-updating-div.aspx 但现在我所有的客户端CIDE验证无法正常工作了。

That What i read about in this article: weblogs.asp/owscott/archive/2010/11/17/mvc-3-ajax-redirecting-instead-of-updating-div.aspx But now all my client cide validation is not working as before.

我的问题是,:如何让阿贾克斯code工作和客户端验证,在同一时间?而这是什么属性关于UnobtrusiveJavaScriptEnabled?是它在它们之间的开关?我希望更多地了解它用简单的话。

My question is: how to make the ajax code work and the client side validations in the same time? and what is this property about "UnobtrusiveJavaScriptEnabled"? is it a switch between them?! I hope to understand more about it in simple words.

推荐答案

在ASP.NET MVC 3有2件事情:客户端验证和不显眼的JavaScript这是由它们的相应值在web.config中控制的:

In ASP.NET MVC 3 there are 2 things: client side validation and unobtrusive javascript which are controlled by their corresponding values in web.config:

<add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />

客户端验证是基于 jquery.validate.js 插件旁边的 jquery.validate.unobtrusive.js 脚本微软。如果包括其中包含一个HTML表单,客户端验证将根据您已经在模型中定义的数据标注规则进行一个视图中这两个脚本。当你看到的视图生成的HTML源代码code,你会发现,输入字段具有HTML5 数据 - * 属性包含验证规则。微软不引人注目的验证脚本将改为这些规则和配置JQuery验证插件。

Client side validation is based on the jquery.validate.js plugin alongside with the jquery.validate.unobtrusive.js script from Microsoft. When you include those two scripts inside a view which contains a HTML form client side validation will be performed based on the data annotation rules you have defined on your model. When you look at the generated HTML source code of the view you will notice that input fields have HTML5 data-* attributes which contain the validation rules. The Microsoft unobtrusive validation script would then read those rules and configure the jquery validate plugin.

在不显眼的JavaScript是不同的。它是基于jquery的。当您使用的阿贾克斯之一。* HTML辅助,例如 Ajax.ActionLink ,在ASP.NET MVC 3,这些助手也发出HTML5 数据 - * 上相应的锚属性。这些属性然后由微软 jquery.unobtrusive-ajax.js 脚本,你需要在你的页面,AJAXify这些链接包括PTED除$ P $。因此,例如,当你写的:

The unobtrusive javascript is different. It is based on jquery. When you use one of the Ajax.* HTML helpers such as Ajax.ActionLink, in ASP.NET MVC 3, those helpers also emit HTML5 data-* attributes on the corresponding anchor. Those attributes are then interpreted by the Microsoft jquery.unobtrusive-ajax.js script which you need to include in your page and AJAXify those links. So for example when you write:

@Ajax.ActionLink("UTC", "GetTime", new { zone = "utc" }, new AjaxOptions { UpdateTargetId = "myResults" })

这会生成以下HTML:

this would generate the following HTML:

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#myResults" href="/Home/GetTime?zone=utc">UTC</a>

正如你可以看到现在所有关于如何执行AJAX请求被包含在DOM中的信息。所以,你可以有一个单独的JavaScript文件,在那里你会认购点击这个链接事件,发送一个AJAX请求包含在的href链接属性,然后根据数据AJAX模式的价值属性替换一些容器的id包含在数据AJAX更新属性选择器。而这正是 jquery.unobtrusive-ajax.js 一样。只是,它是在一个单独的文件和你的标记和JavaScript是独立这是不是在previous版本的情况。

As you can see now all the information about how to perform the AJAX request is contained in the DOM. So you could have a separate javascript file where you would subscribe for the click event of this link, send an AJAX request to the url contained in the href attribute and then based on the value of the data-ajax-mode attribute replace the html of some container with id contained in the data-ajax-update attribute selector. And that's exactly what the jquery.unobtrusive-ajax.js does. It's just that it is in a separate file and your markup and javascript are independent which wasn't the case in previous versions.

所以违背ASP.NET MVC 1和2,在ASP.NET MVC 3 jQuery是JavaScript框架和HTML助手是基于它的默认值。所有 MicrosoftAjax * 脚本不再使用。

So contrary to ASP.NET MVC 1 and 2, in ASP.NET MVC 3 jQuery is the default javascript framework and HTML helpers are based on it. All MicrosoftAjax* scripts are no longer used.

更多推荐

为什么UnobtrusiveJavaScriptEnabled = true来禁用我的AJAX工作吗?

本文发布于:2023-11-24 21:19:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   UnobtrusiveJavaScriptEnabled   true   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!