C# ASP.Net 2012 Unobtrusive Validation with jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12452109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.Net 2012 Unobtrusive Validation with jQuery
提问by Jupaol
I was playing with Visual Studio 2012 and I created an empty ASP.Net Web Application, when I tried to add the traditional validator controlsto a new page, this error occurs:
我在玩 Visual Studio 2012 并创建了一个空的 ASP.Net Web Application,当我尝试将传统验证器控件添加到新页面时,会发生此错误:
WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).
WebForms UnobtrusiveValidationMode 需要“jquery”的 ScriptResourceMapping。请添加一个名为 jquery(区分大小写)的 ScriptResourceMapping。
What are the steps to fix it?
修复它的步骤是什么?
This is my page markup:
这是我的页面标记:
<asp:Panel runat="server" ID="pnlUsername" GroupingText="Username settings">
<div>
<asp:Label ID="usernameLabel" Text="Please enter your username" runat="server" AssociatedControlID="username" />
</div>
<div>
<asp:TextBox runat="server" ID="username" />
<asp:RequiredFieldValidator ErrorMessage="The username is required" ControlToValidate="username" runat="server" Text=" - Required" />
</div>
</asp:Panel>
回答by Jagmag
This is the official Microsoft answer from the MS Connect forums. I am copying the relevant text below :-
这是来自 MS Connect 论坛的 Microsoft 官方回答。我正在复制以下相关文字:-
When targeting .NET 4.5 Unobtrusive Validation is enabled by default. You need to have jQuery in your project and have something like this in Global.asax to register jQuery properly:
面向 .NET 4.5 时,默认启用非侵入式验证。你需要在你的项目中有 jQuery 并且在 Global.asax 中有这样的东西来正确注册 jQuery:
ScriptManager.ScriptResourceMapping.AddDefinition("jquery",
new ScriptResourceDefinition {
Path = "~/scripts/jquery-1.4.1.min.js",
DebugPath = "~/scripts/jquery-1.4.1.js",
CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
});
Replacing the version of jQuery with the version you are using.
用您正在使用的版本替换 jQuery 版本。
You can also disable this new feature in web.config by removing the following line:
您还可以通过删除以下行在 web.config 中禁用此新功能:
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
回答by Davit Tvildiani
<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />
this line was not in my WebConfig so : I simple solved thisby downgrading targetting .Net version to 4.0 :)
这行不在我的 WebConfig 中,所以:我通过将目标 .Net 版本降级到 4.0 来简单地解决了这个问题:)
回答by rlamoni
It seems like there is a lot of incorrect information about the ValidationSettings:UnobtrusiveValidationMode value. To Disableit you need to do the following.
似乎有很多关于 ValidationSettings:UnobtrusiveValidationMode 值的错误信息。要禁用它,您需要执行以下操作。
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
The word None, not WebForms should be used to disable this feature.
应使用 None 而不是 WebForms 一词来禁用此功能。
回答by Kapil Khandelwal
More Info on ValidationSettings:UnobtrusiveValidationMode
有关ValidationSettings:UnobtrusiveValidationMode 的更多信息
Specifies how ASP.NET globally enables the built-in validator controls to use unobtrusive JavaScript for client-side validation logic.
指定 ASP.NET 如何全局启用内置验证器控件以将不显眼的 JavaScript 用于客户端验证逻辑。
Type: UnobtrusiveValidationMode
类型:UnobtrusiveValidationMode
Default value: None
默认值:无
Remarks: If this key value is set to "None"[default], the ASP.NET application will use the pre-4.5 behavior(JavaScript inline in the pages) for client-side validation logic. If this key value is set to "WebForms", ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script referencefor client-side validation logic.
备注:如果此键值设置为“无”[默认],则 ASP.NET 应用程序将使用4.5 之前的行为(页面中的 JavaScript 内联)进行客户端验证逻辑。如果此键值设置为"WebForms",则 ASP.NET 使用HTML5 数据属性和来自客户端验证逻辑的添加脚本引用的后期绑定 JavaScript。
Example:
例子:
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
回答by Nikola Bogdanovi?
Other than the required "jquery" ScriptResourceDefinitionin Global.asax (use your own paths):
除了ScriptResourceDefinitionGlobal.asax 中所需的“jquery” (使用您自己的路径):
protected void Application_Start(object sender, EventArgs e)
{
ScriptManager.ScriptResourceMapping.AddDefinition(
"jquery",
new ScriptResourceDefinition
{
Path = "/static/scripts/jquery-1.8.3.min.js",
DebugPath = "/static/scripts/jquery-1.8.3.js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.js",
CdnSupportsSecureConnection = true,
LoadSuccessExpression = "jQuery"
});
}
You additionally only need to explicitly add "WebUIValidation.js" after"jquery" ScriptReferencein ScriptManager(the most important part):
您还只需要在(最重要的部分)中的“jquery”之后显式添加“WebUIValidation.js” :ScriptReferenceScriptManager
<asp:ScriptManager runat="server" EnableScriptGlobalization="True" EnableCdn="True">
<Scripts>
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" />
</Scripts>
</asp:ScriptManager>
If you add it before "jquery", or if you don't add any or both of them at all (ASP.Netwill then automatically add it before "jquery") - the client validation will be completely broken:
如果您在“jquery”之前添加它,或者根本不添加任何一个或两个(ASP.Net然后会自动将其添加在“jquery”之前) - 客户端验证将完全中断:
You don't need any of those NuGet packages at all, nor any additional ScriptReference(some of which are just duplicates, or even a completely unnecessary bloat - as they are added automatically by ASP.Netif needed) mentioned in your blog.
您根本不需要这些 NuGet 包中的任何一个,也不需要您的博客中提到的任何其他包ScriptReference(其中一些只是重复的,甚至是完全不必要的膨胀 - 因为它们会ASP.Net根据需要自动添加)。
EDIT:you don't need to explicitly add "WebForms.js" as well (removed it from the example) - and if you do, its LoadSuccessExpressionwill be ignored for some reason
编辑:您也不需要显式添加“WebForms.js”(将其从示例中删除) - 如果您这样做,它LoadSuccessExpression会因某种原因被忽略
回答by roberto
in visual studio 2012 in the web.config change the targetFramework=4.5 to targetFramework=4.0
在 web.config 中的 Visual Studio 2012 中,将 targetFramework=4.5 更改为 targetFramework=4.0
回答by Brijesh Bajpai
Add a reference to Microsoft.JScriptin your application in your web.configas below :
Microsoft.JScript在您的应用程序中添加对以下内容的引用web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="Microsoft.JScript, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5"/>
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none"/>
</appSettings>
</configuration>
回答by Nimit Joshi
Just copy & paste any JQuery file in your project and add a Global.asaxfile and modify it as below:
只需将任何 JQuery 文件复制并粘贴到您的项目中,然后添加一个Global.asax文件并进行如下修改:
I just paste the JQuery file in my project and add a reference in Global.asax file:
我只是将 JQuery 文件粘贴到我的项目中并在 Global.asax 文件中添加一个引用:
protected void Application_Start(object sender, EventArgs e)
{
ScriptManager.ScriptResourceMapping.AddDefinition(
"jquery",
new ScriptResourceDefinition
{
Path = "~/jquery-1.10.2.js",
DebugPath = "~/jquery-1.10.2.js",
CdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js",
CdnDebugPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.js",
CdnSupportsSecureConnection = true,
LoadSuccessExpression = "jQuery"
});
}
回答by Rusty
In the Nuget Package manager search for AspNet.ScriptManager.jQuery instead of jquery. THat way you will not have to set the mappings yourself and the project will work with the click of a simple install.
在 Nuget 包管理器中搜索 AspNet.ScriptManager.jQuery 而不是 jquery。这样您就不必自己设置映射,只需单击一个简单的安装,项目就可以工作。
Or disable the Unobtrusive Validation by adding this line to the Configuration tag of the web.config file in the project.
或者通过将此行添加到项目中 web.config 文件的 Configuration 标记来禁用 Unobtrusive Validation。
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
回答by Hema Shetty
All the validator error has been solved by this
所有验证器错误都已由此解决
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
Error must be vanished enjoy....
错误必须消失享受....

