Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net: Do I need to add javascript source of _ValidationScriptsPartial.cshtml to _layout.cshtml?

Tags:

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?

like image 997
John Avatar asked Sep 18 '15 06:09

John


People also ask

What is _ValidationScriptsPartial Cshtml?

_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.

What is @{ await HTML RenderPartialAsync _ValidationScriptsPartial );}?

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).

Which of the following code is used to add jquery into layout Cshtml?

The jquery code is in _Layout,cshtml.

Can we have multiple _ViewStart in MVC?

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.


1 Answers

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

like image 199
Joe Audette Avatar answered Oct 10 '22 05:10

Joe Audette