For data validation, where to add Html.RenderPartialAsync(“_ValidationScriptsPartial”)?

不想你离开。 提交于 2019-12-13 20:23:29

问题


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"); }
}

回答1:


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://docs.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.



来源:https://stackoverflow.com/questions/46006910/for-data-validation-where-to-add-html-renderpartialasync-validationscriptspar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!