c# – WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

c# – WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Since .NET 4.5 the Validators use data-attributes and bounded Javascript to do the validation work, so .NET expects you to add a script reference for jQuery.

There are two possible ways to solve the error:


Disable UnobtrusiveValidationMode:

Add this to web.config:

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

It will work as it worked in previous .NET versions and will just add the necessary Javascript to your page to make the validators work, instead of looking for the code in your jQuery file. This is the common solution actually.


Another solution is to register the script:

In Global.asax Application_Start add mapping to your jQuery file path:

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    ScriptManager.ScriptResourceMapping.AddDefinition(jquery, 
    new ScriptResourceDefinition
    {
        Path = ~/scripts/jquery-1.7.2.min.js,
        DebugPath = ~/scripts/jquery-1.7.2.js,
        CdnPath = http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.min.js,
        CdnDebugPath = http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.1.js
    });
}

Some details from MSDN:

ValidationSettings:UnobtrusiveValidationMode Specifies how ASP.NET
globally enables the built-in validator controls to use unobtrusive
JavaScript for client-side validation logic.

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 reference for client-side validation logic.

To fix this problem on specific page need to set some validation settings when page loading. Write code below in Page_Load() method:

protected void Page_Load(object sender, EventArgs e)
    {
        ValidationSettings.UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
    }

Its work for me in .NET 4.5

c# – WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

I think this is the best solution for this type error.
So please add below line.
Also it work my code when I am using MSVS 2015.

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

Leave a Reply

Your email address will not be published.