为了进行数据验证,在何处添加Html.RenderPartialAsync("_ ValidationScriptsPartial")? [英] For data validation, where to add Html.RenderPartialAsync("_ValidationScriptsPartial")?

查看:853
本文介绍了为了进行数据验证,在何处添加Html.RenderPartialAsync("_ ValidationScriptsPartial")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET Core 1.1.1应用中,模型验证不起作用.我注意到一些默认的views(例如login.cshtmlRegister.cshtml是在创建应用程序时由VS2017创建的)在末尾具有以下代码.但是这些默认视图实际上是局部视图.我的视图不是局部视图,是否还应在views的末尾添加以下内容?或者我应该在不是局部视图的视图末尾添加什么内容:

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

推荐答案

嗯,

@section Scripts {
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

用于客户端验证"(javascript).如果表单无效(根据模型验证),则不允许用户发送该表单.

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

如果打开/Views/Shared/_Layout.cshtml,则会在其底部看到以下代码:

If you open your /Views/Shared/_Layout.cshtml you´ll see at the bottom of it the following code:

@RenderSection("Scripts", required: false)

在此代码块中,将插入@section Scripts中的内容,在这种情况下,将是部分视图_ValidationScriptsPartial的内容.

This code block is where the content from @section Scripts will be injected, in these case, the content of the Partial View _ValidationScriptsPartial.

required: false一样,如果您的视图不需要客户端验证,则无需添加@section Scripts代码.

As required: false, if your view does not need client validation you does not need to add the @section Scripts code.

关于_ValidationScriptsPartial视图名称中的Partial,它意味着视图本身是局部的,不能直接使用.请勿将其与应在部分视图中使用"相混淆.

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

更多信息:

https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout#sections

注入内容从带有Razor View Engine的局部视图ASP.NET MVC 3进入特定部分

致谢.

这篇关于为了进行数据验证,在何处添加Html.RenderPartialAsync("_ ValidationScriptsPartial")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆