10

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

David van Dugteren
  • 3,238
  • 8
  • 31
  • 47
  • is there Ajax library in json.package? – Bibek Aryal Jun 21 '17 at 04:03
  • 1
    There is no json.package, but there is a bower.json and its an included dependency – David van Dugteren Jun 21 '17 at 05:07
  • im not sure about this but im only writing this because you havent really got any other responses. dot net core needs to have ajax library and ajax wont be accepted by default unlike that in MVC. try adding it in bower itself or just install the nuget package – Bibek Aryal Jun 21 '17 at 08:43

2 Answers2

16

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

Nick Albrecht
  • 15,828
  • 8
  • 64
  • 95
0

What is the equlivalent of OnSuccess

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