简体   繁体   English

使用jQuery的ASP.Net 2012不干扰验证

[英]ASP.Net 2012 Unobtrusive Validation with jQuery

I was playing with Visual Studio 2012 and I created an empty ASP.Net Web Application , when I tried to add the traditional validator controls to a new page, this error occurs: 我正在使用Visual Studio 2012,并且创建了一个空的ASP.Net Web应用程序 ,当我尝试将传统的验证器控件添加到新页面时,发生此错误:

WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. WebForms UnobtrusiveValidationMode需要针对“ jquery”的ScriptResourceMapping。 Please add a ScriptResourceMapping named jquery(case-sensitive). 请添加一个名为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>

It seems like there is a lot of incorrect information about the ValidationSettings:UnobtrusiveValidationMode value. 似乎有许多关于ValidationSettings:UnobtrusiveValidationMode值的错误信息。 To Disable it you need to do the following. 禁用它,您需要执行以下操作。

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

The word None, not WebForms should be used to disable this feature. 应该使用单词None(不是WebForms)来禁用此功能。

This is the official Microsoft answer from the MS Connect forums . 这是微软在MS Connect论坛上提供官方答案 I am copying the relevant text below :- 我正在复制以下相关文本:

When targeting .NET 4.5 Unobtrusive Validation is enabled by default. 面向.NET 4.5时,默认启用非侵入式验证。 You need to have jQuery in your project and have something like this in Global.asax to register jQuery properly: 您需要在项目中安装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" />

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. 备注:如果此键值设置为“无” (默认值),则ASP.NET应用程序将使用4.5之前的行为 (页面中的JavaScript内联)进行客户端验证逻辑。 If this key value is set to "WebForms" , ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script reference for client-side validation logic. 如果此键值设置为“ WebForms” ,则ASP.NET将使用HTML5数据属性和来自添加的脚本参考的后期绑定JavaScript进行客户端验证逻辑。

Example: 例:

    <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>

Other than the required "jquery" ScriptResourceDefinition in Global.asax (use your own paths): Global.asax中所需的“ jquery” ScriptResourceDefinition除外(使用您自己的路径):

    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" ScriptReference in ScriptManager (the most important part): 另外,您只需要在ScriptManager “ jquery” ScriptReference 之后显式添加“ WebUIValidation.js”(最重要的部分):

       <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.Net will then automatically add it before "jquery") - the client validation will be completely broken: 如果在“ jquery”之前添加它,或者根本不添加任何一个或两个( ASP.Net会在“ jquery”之前自动添加它)-客户端验证将被完全破坏:

http://connect.microsoft.com/VisualStudio/feedback/details/748064/unobtrusive-validation-breaks-with-a-script-manager-on-the-page http://connect.microsoft.com/VisualStudio/feedback/details/748064/unobtrusive-validation-breaks-with-a-script-manager-on-the-page

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.Net if 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 LoadSuccessExpression will be ignored for some reason 编辑:您也不需要显式添加“ WebForms.js”(从示例中将其删除)-如果这样做,由于某些原因,它的LoadSuccessExpression将被忽略。

All the validator error has been solved by this 所有的验证器错误都已经解决了

 <appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>

Error must be vanished enjoy.... 错误必须消失享受。

Add a reference to Microsoft.JScript in your application in your web.config as below : 在您的应用程序的web.config添加对Microsoft.JScript的引用,如下所示:

<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>

Just copy & paste any JQuery file in your project and add a Global.asax file 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"
        });
    }

In the Nuget Package manager search for AspNet.ScriptManager.jQuery instead of jquery. 在Nuget软件包管理器中,搜索AspNet.ScriptManager.jQuery而不是jquery。 THat way you will not have to set the mappings yourself and the project will work with the click of a simple install. 通过这种方式,您不必自己设置映射,项目只需单击一下即可安装。

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" />

In "configuration file" instead this lines: 在“配置文件”中改为:

<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />

by this lines: 通过此行:

<compilation debug="true" targetFramework="4.0" />
<httpRuntime targetFramework="4.0" />

This error because in version 4.0 library belong to "asp:RequiredFieldValidator" exist but in version 4.5 library not exist so you need to add library by yourself 出现此错误的原因是,在版本4.0中,库属于“ asp:RequiredFieldValidator”,但是在版本4.5中库不存在,因此您需要自己添加库

<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />

此行不在我的WebConfig中,因此:我通过将目标.Net版本降级为4.0 来解决此问题 :)

在Visual Studio 2012中的web.config中,将targetFramework = 4.5更改为targetFramework = 4.0

I suggest to instead this lines 我建议改成这行

<div>
    <asp:TextBox runat="server" ID="username" />
    <asp:RequiredFieldValidator ErrorMessage="The username is required" ControlToValidate="username" runat="server" Text=" - Required" />
</div>

by this line 通过这条线

<div>
    <asp:TextBox runat="server" ID="username" required />
</div>

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

相关问题 不引人注目的ASP.NET MVC自定义验证 - ASP.NET MVC Custom Validation with Unobtrusive asp.net核心中的自定义标签帮助程序和轻松的验证 - Custom tag helper and unobtrusive validation in asp.net core 在页面提交时在asp.net中添加不打扰的验证 - Add unobtrusive validation in asp.net on submit of page ASP.NET MVC4不显眼的验证本地化 - ASP.NET MVC4 Unobtrusive validation localization ASP.NET MVC 4.0中非侵入式验证中的奇怪行为 - Strange behavior in Unobtrusive validation in ASP.NET MVC 4.0 即使提交失败,ASP.NET Core Unobtrusive客户端验证表单onsubmit也会触发 - ASP.NET Core Unobtrusive Client Side Validation Form onsubmit fires even on validation failure ASP.NET Core 2.1 不显眼的 Ajax 验证不适用于部分视图表单交换 - ASP.NET Core 2.1 Unobtrusive Ajax Validation Not Working With Partial View Form Swap 视图模型无法在服务器端进行验证后,未调用ASP.NET MVC 3非侵入式验证onError - ASP.NET MVC 3 unobtrusive validation onError not called after view model fails to validate on server side 使用fluentvalidation和asp.net mvc LessThanOrEqualTo不触发的不显眼的客户端验证 - unobtrusive client validation using fluentvalidation and asp.net mvc LessThanOrEqualTo not firing 在 ASP.NET Core MVC 中不工作的 bool 字段的不显眼的客户端验证 - Unobtrusive client-side validation for bool field not working in ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM