ASP.NET Core and JQuery unobtrusive AJAX not worki

2020-05-27 03:58发布

If I start a new project in .net 4.5 and hit manage nuget packages, search for ajax, then hit install for ajax unobtrusive. I can then go into my cshtml file and type @Ajax.___ e.g. @Ajax.beginForm

If I create a new project with Dot Net Core... I cannot, where instead it gives me the useless error:

"The name 'Ajax' does not exist in the current context"

I've searched the web, found nothing...

Basically I want to make a Form with an Ajax call as you could do in .net

Such as:

"@using (Ajax.BeginForm("EmployeeMaster", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" })) {"

Anyone know what else I can try?

In the above, I can see that Ajax is a object of type AjaxHelper which comes from System.web.mvc.webviewpage... so maybe its never meant to be available for .net core

2条回答
爷的心禁止访问
2楼-- · 2020-05-27 04:26

What is the equlivalent of OnSuccess

@using (Ajax.BeginForm("EmployeeMaster", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp", OnSuccess="ShowMessage" }))
{
    //form content
}
查看更多
手持菜刀,她持情操
3楼-- · 2020-05-27 04:49

While the Ajax.BeginForm() methods do not work, the actual unobtrusive Ajax html does. The C# methods just help generate the Html form tags. Forgive me in thse syntax is wrong, this is from memory, but you get the idea

@using (Ajax.BeginForm("EmployeeMaster", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp" }))
{
    //form content
}

becomes

<form asp-action="EmployeeMaster" asp-controller="Home" method="POST" data-ajax="true" data-ajax-update="divEmp">
    <!-- form content-->
</form>

NOTE: The above HTML STILL needs the unobtrusive ajax js files for it to wire up these data-* attributes correctly. This is not a part of ASP.NET Core MVC

查看更多
登录 后发表回答