[英]how to enable UnobtrusiveJavaScriptEnabled in .NET6?
In .NET Framework,Have you enabled client side validation in the web.config:在 .NET 框架中,您是否在 web.config 中启用了客户端验证:
<appSettings>
...
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Or enabled it through the HTML Helper:或者通过 HTML Helper 启用它:
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
how to enable UnobtrusiveJavaScriptEnabled in .NET6?如何在 .NET6 中启用 UnobtrusiveJavaScriptEnabled? Is there a need to do this?
有必要这样做吗?
In ASP.NET Core MVC/Razor Pages, it contains jquery.validate.js
and jquery.validate.unobtrusive.js
file by default in wwwroor/lib
folder, but you need add reference to the view to enable them. In ASP.NET Core MVC/Razor Pages, it contains
jquery.validate.js
and jquery.validate.unobtrusive.js
file by default in wwwroor/lib
folder, but you need add reference to the view to enable them.
If you just want enable them in specific view , you can use these scripts and just add the partial view inside the .cshtml
file like this:如果您只想在特定视图中启用它们,您可以使用这些脚本并在
.cshtml
文件中添加部分视图,如下所示:
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}
If you want to enable them in every view , you can add it in _Layout.cshtml
like below:如果要在每个视图中启用它们,可以将其添加到
_Layout.cshtml
中,如下所示:
//.....
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
//add this reference here.....
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
If you also want to use Ajax.ActionLink
in ASP.NET Core, this way is only apply to ASP.NET MVC 5.2.如果您还想在 ASP.NET 内核中使用
Ajax.ActionLink
,这种方式仅适用于 ASP.NET MVC 5.2。 In ASP.NET 6, you need use an alternative way.在 ASP.NET 6 中,您需要使用替代方式。
For example:例如:
In ASP.NET MVC 5.2, you use在 ASP.NET MVC 5.2 中,您使用
@{
AjaxOptions deleteOptions = new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "table"
};
}
@Ajax.ActionLink("Delete", "ActionName", "ControllerName", new { id = 1 }, deleteOptions)
In ASP.NET 6, you can use在 ASP.NET 6 中,您可以使用
<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#table" data-ajax-url="@Url.Action("ActionName", "ControllerName", new { id = 1})">Delete</a>
Also remember to add the jquery.unobtrusive-ajax.js
reference in your view:还要记住在您的视图中添加
jquery.unobtrusive-ajax.js
引用:
@section Scripts
{
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-ajax-unobtrusive/3.2.6/jquery.unobtrusive-ajax.js" integrity="sha256-v2nySZafnswY87um3ymbg7p9f766IQspC5oqaqZVX2c=" crossorigin="anonymous"></script>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.