Reputation: 11
I was trying to implement an asynchronous call with ajax in my aspnet core 2.0 project, using "Ajax.BeginForm", but then I discovered according to these threads on github and stackoverflow that there's no support to this class in aspnet core, nor MVC 6.
Is there any other way to use ajax assynchronous calls, in aspnet core, besides the old way, with $.ajax
function? My problem with this, is that, with AjaxHelper
class, I could do everything with razor, in the view file, and keeping everything organized in one place.
Why would i use the razor language to make a loop, and print a list, or whatever, if i have to to make the ajax call, in the js file (or in a < script > tag)? I could work with the data right there, with js, and keep everything there!
How are you guys solving this right now? Thanks for your help.
Upvotes: 1
Views: 957
Reputation: 9821
This baffled me a bit too. I was like where is my lovely @Ajax helper that saved me allot of time in JS hell.
As @sam linked they have changed the API to be more HTML 5 semantic - Not sure why but OK
<form asp-controller="Home" asp-action="SaveForm"
data-ajax-begin="onBegin" data-ajax-complete="onComplete"
data-ajax-failure="onFailed" data-ajax-success="onSuccess"
data-ajax="true" data-ajax-method="POST">
<input type="submit" value="Save" class="btn btn-primary" />
<div id="Results"></div>
</form>
You can also do the same on anchors and don't forgot the tag to enable it
<a data-ajax="true" data-ajax-begin="alert('Hello!')">TEST</a>
And then I suppose you could create TypeScript files per page similar to Angular or one TypeScript per site, or just put the script on the page, example
var onSuccess = function(context){
alert(context);
};
Upvotes: 1