In 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>
below part of _layout.cshtml
:
<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 VALIDATION HERE </script> </environment>
What is the purpose of _ValidationScriptsPartial.cshtml
? How is this file used in the project? Please give me reference how to use this file?
_ValidationScriptsPartial.cshtml. This file contains validation scripts in the form of a partial view. By default, this file contains the below code: <environment include="Development"> <script src="~/lib/jquery-validation/dist/jquery.
Well, @section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } } is used for "client side validation" (javascript). It does not let the user send the form if it is not valid (according to Model Validation).
The jquery code is in _Layout,cshtml.
We can also create multiple _ViewStart. cshtml pages. The file execution is dependent upon the location of the file within the folder hierarchy and the view being rendered. The MVC Runtime will first execute the code of the _ViewStart.
Partial Views are meant to be used inside other views. You would not normally add the validation scripts to the _layout.cshtml
since that (_layout.cshtm
) is used on every page.
However, if you have a view where you need to use these scripts you just add the partial view inside the .cshtml
file of your view like this:
@section Scripts { @{await Html.RenderPartialAsync("_ValidationScriptsPartial"); } }
If you created a standard web project with identity using VS 2015 then you can see an example of this usage in Views/Account/Register.cshtml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With