简体   繁体   English

如何在 .NET6 中启用 UnobtrusiveJavaScriptEnabled?

[英]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.

相关问题 UnobtrusiveJavaScriptEnabled键在.NET中的作用是什么? - What does the UnobtrusiveJavaScriptEnabled key do in .NET? 根据视图.NET6 EF6 Razor Pages No MVC中模型项的值更改div的innerHtml - Change innerHtml of div according to value of model item in view .NET6 EF6 Razor Pages No MVC Web.config:ClientValidationEnabled和UnobtrusiveJavaScriptEnabled的值? - Web.config: Values for ClientValidationEnabled and UnobtrusiveJavaScriptEnabled? 如何在asp.net Web表单应用程序中“启用CORS”? - How to 'Enable CORS' in asp.net web form application? 如何在单选按钮单击上启用/禁用asp.net验证器? - how to enable/disable asp.net validators on radio button click? 如何在asp.net中的代码后面启用禁用的按钮 - How to enable a disabled button in code behind in asp.net 如何在ASP.NET中自动启用浏览器位置设置 - How to automatically enable browser location setting in asp.net 如何使用Javascript禁用/启用asp.net计时器控件? - How to disable/enable asp.net timer control using Javascript? 如何为多个动态文本框(ASP.Net)启用Google音译 - How to enable Google Transliteration for multiple dynamic Textboxes (ASP.Net) 如何在JavaScript中启用或禁用asp.net验证器 - how to enable or disable asp.net validators in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM