4

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"); }
}
nam
  • 21,967
  • 37
  • 158
  • 332
  • When you say that "is not working" what do you mean? It can be "not working" on client (javascript), "not working" on server (can be many things). What exactly is happening (define "not working")? – dime2lo Sep 02 '17 at 03:28
  • @dime2lo It's not working on client-side. The details are on my this [post](https://stackoverflow.com/q/46008588/1232087). – nam Sep 02 '17 at 04:17

1 Answers1

10

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.

dime2lo
  • 826
  • 8
  • 15
  • You've answered my question except that I was also confused about whether `@section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } }` can be added to any view that needs client-side validation since the word `partial` in the code above was making me think the above code is meant for partial views only. But I think I did not put it correctly in the post. For the benefit of other readers you may want to add a one liner in your `Reply` in regard to my confusion. And I'll mark that as an `Answer`. – nam Sep 04 '17 at 17:09
  • 1
    Have edited the answer, so others don´t get confused about the "Partial" in view´s name. (Added: 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"). – dime2lo Sep 04 '17 at 19:25
  • This is a great answer. It was spot on with what I needed. Thank you @dime2lo. –  Dec 17 '20 at 14:47