Sergey Sergey - 3 months ago 14
ASP.NET (C#) Question

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

i'm using asp.net 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:

works:

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


MyModel contains only my models properties.

exmple:

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.

exmple:

public class VM
{
public MyModel MyModel
public CustomClass OtherClass
...
}


Why it doesn't show validation errors?

Answer

I found a solution here: https://github.com/aspnet/jquery-validation-unobtrusive/issues/39#issuecomment-237945087

@rok7

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 http://api.jquery.com/category/selectors/

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

function escapeAttributeValue(value) {

    // As mentioned on http://api.jquery.com/category/selectors/

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

@ryanbrandenburg

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.

Comments