3

In .NET Framework,Have you enabled client side validation in the web.config:

<appSettings>
 ...
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

Or enabled it through the HTML Helper:

HtmlHelper.UnobtrusiveJavaScriptEnabled = true;

how to enable UnobtrusiveJavaScriptEnabled in .NET6? Is there a need to do this?

2
  • it is enable as default. did you check validation scripts? Commented Sep 3, 2022 at 12:04
  • @OkanKaradag When I use the ajaxlink in .NET6 MVC Razor,I get Uncaught ReferenceError: Sys is not defined error. It is because of the disable UnobtrusiveJavaScriptEnabled.Other cases have been observed. Commented Sep 3, 2022 at 12:28

1 Answer 1

2

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:

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

If you want to enable them in every view, you can add it in _Layout.cshtml like below:

    //.....
    <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. In ASP.NET 6, you need use an alternative way.

For example:

In ASP.NET MVC 5.2, you use

@{
    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

<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:

@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>

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.