Do I need to add _ValidationScriptsPartial.cshtml javascript source to _layout.cshtml?

In an Asp.net 5 project, I have a file named _ValidationScriptsPartial.cshtml by default,

<environment names="Development"> <script src="~/lib/jquery-validation/jquery.validate.js"></script> <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script> </environment> <environment names="Staging,Production"> <script src="//ajax.aspnetcdn.com/ajax/jquery.validation/1.11.1/jquery.validate.min.js" asp-fallback-src="~/lib/jquery-validation/jquery.validate.js" asp-fallback-test="window.jquery && window.jquery.validator"> </script> <script src="//ajax.aspnetcdn.com/ajax/mvc/5.2.3/jquery.validate.unobtrusive.min.js" asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js" asp-fallback-test="window.jquery && window.jquery.validator && window.jquery.validator.unobtrusive"> </script> </environment> 

But when I need to use jquery validation, I have to add

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

to the _layout.cshtml part.

 <environment names="Development"> <script src="~/lib/jquery/dist/jquery.js"></script> <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script> <script src="~/lib/hammer.js/hammer.js"></script> <script src="~/lib/bootstrap-touch-carousel/dist/js/bootstrap-touch-carousel.js"> I HAVE TO ADD SCRIPT FOR JQUERY VARIDATION HERE </script> </environment> 

Then what is the purpose of _ValidationScriptsPartial.cshtml? How is this file used in the project? Please give me a link, how to use this file?

+6
source share
1 answer

Partial views are intended to be used inside other views. Usually, you did not add validation scripts to the _layout.cshtml file, since it is used on every page, but if you have a view where you need to use these scripts, you simply add a partial view inside the .cshtml file of your view as follows:

 @section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial"); } } 

If you created a standard web project with an identifier using VS 2015, you can see an example of this use in Views / Account / Register.cshtml

+9
source

All Articles