In my ASP.NET Core 1.1.1
app the model validation is not working. I've noticed that some of the default views
(such as login.cshtml
, Register.cshtml
that were created by VS2017
when the app was created) have the following code at the end. But these default views are in fact partial views. My Views are not partial views, should the following be added to end of my views
as well? Or what should I be adding to the end of my views that are not partial views:
@section Scripts {
@{ 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).
If you open your /Views/Shared/_Layout.cshtml you´ll see at the bottom of it the following code:
@RenderSection("Scripts", required: false)
This code block is where the content from @section Scripts
will be injected, in these case, the content of the Partial View _ValidationScriptsPartial
.
As required: false
, if your view does not need client validation you does not need to add the @section Scripts
code.
Regarding the Partial
in _ValidationScriptsPartial
view name it means that the view itself is partial, it is not intended to be served directly. It must not be confused with "it should be used in partial views".
More info:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/layout#sections
Injecting content into specific sections from a partial view ASP.NET MVC 3 with Razor View Engine
Regards.