Sergey Sergey - 1 year ago 64
ASP.NET (C#) Question

jquery unobtrusive stops working if asp-for has dots in name

i'm using core with mvc 6 and jquery-validation-unobtrusive js library. The thing is unobtrusive js library doesn't check my input when i submit my form.

everething is pretty simple, here's basic example:


@model MyModel
<input asp-for="MyField" placeholder="...">
<span asp-validation-for="MyField"></span>

MyModel contains only my models properties.


public class MyModel
public string MyField
public string MyField2

not working:

@model VM
<input asp-for="MyModel.MyField" placeholder="...">
<span asp-validation-for="MyModel.MyField"></span>

VM contains MyModel class plus other properties.


public class VM
public MyModel MyModel
public CustomClass OtherClass

Why it doesn't show validation errors?

Answer Source

I found a solution here:


While trying to provide an application for investigation I found the problem: When a new ASP.Net Core application is created everything works ok. However, if in VisualStudio the file bower.json is manually modified (because jquery-validation-unobtrusive is not listed by the ‘Browse’ function, it's necessary for example to select a different version), then VisualStudio restores jquery-validation-unobtrusive with the following difference to the original file : Original:

function escapeAttributeValue(value) {

    // As mentioned on

    return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
} VisualStudio-restored

function escapeAttributeValue(value) {

    // As mentioned on

    return value.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\\\$1");
} These magically inserted ‘\’ caused my problems!!!


Those \ were the result of an attempt to fix a different issue, but upon closer inspection that issue was caused by something else and we can revert that change. That's happening in #44.

Shortly a workaround of this situation would be a manual fixing the mentioned line or a downgrading to 3.2.5 version.

I believe it will be fixed in the next release.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download