Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core: Unobtrusive validation not working

The client side validation in ASP.Net Core (Unlike ASP.Net MVC 5) is not working. I have the following code:

public class CountryModel{
    [Required]
    public String Title {get; set;}
}

and in my view page

using(Html.BeginForm()){
   @Html.LabelFor(x=> x.Title)
   @Html.ValidationMessageFor(x=> x.Title)
   @Html.EditorFor(x=> x.Title)
   <Button>Add</Button>
}

with the following scripts added to the end of the page:

...
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>  
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>

But it fails. I have check the script through viewing the source in my browser and the scripts have been loaded properly. I have pointed a break point to my Action API to double check, and any time the add button is pressed I reach the break point meaning the client side validation fails.

Is there any additional configuration in ASP.Net Core?? Because this approach works just fine in MVC 5.

like image 684
Arnold Zahrneinder Avatar asked Jan 05 '17 23:01

Arnold Zahrneinder


People also ask

What is ModelState in asp net core?

When we talk about ModelState , we mean ModelState property of the ControllerBase abstract class in the Microsoft. AspNetCore. Mvc namespace. It is of ModelStateDictionary type and it represents errors that come from two subsystems: model binding and model validation.

What is validator unobtrusive parse?

validator. unobtrusive. parse(selector) method to force parsing. This method parses all the HTML elements in the specified selector and looks for input elements decorated with the [data-val=true] attribute value and enables validation according to the data-val-* attribute values.

What is _ValidationScriptsPartial?

_ValidationScriptsPartial. cshtml exists to reference validation scripts but the same validation scripts can be references from _Layout. cshtml just as well. What is the rationale/benefit of separating them out to _ValidationScriptsPartial. cshtml ?


2 Answers

Please make sure your validation-related scripts are placed under the environment which you are using. Development Or Production.

I was struggling for some time and when I checked my script it was placed in the development section and my app was running using the production environment.

Once I placed the scripts under production it started working !

like image 180
Suresh M Avatar answered Oct 05 '22 19:10

Suresh M


In my case the problem was quite different.

The validation scripts references were located in the default/built in _ValidationScriptsPartial.cshtml that I was just adding to my page by adding

@section Scripts 
{
     <partial name="_ValidationScriptsPartial"/>    
}

at the end of my View cshtml.

I could see that it was referenced properly in Developer Tools, so I thought it's all fine:

enter image description here

However, it turned out that the paths in _ValidationScriptsPartial were not correct! As you can see, there is only minified version available... and in the case of the second one, a dist subfolder is missing.

enter image description here

It was quite surprising, giving this was a boilerplate code that I did not touch.

like image 27
Bartosz Avatar answered Oct 05 '22 21:10

Bartosz