I think in asp.net MVC core we no more have the option to use Ajax.BeginForms .. so what is the alternative for Ajax.BeginForm ?
I think in asp.net MVC core we no more have the option to use Ajax.BeginForms .. so what is the alternative for Ajax.BeginForm ?
You can use inline data-ajax-* attributes :
<form method="get"
data-ajax-="true"
data-ajax-method="get"
data-ajax-url="/test">
...
</form>
data-ajaxdata-ajax-urldata-ajax-methoddata-ajax-modedata-ajax-updatedata-ajax-loadingdata-ajax-loading-durationdata-ajax-confirmdata-ajax-begindata-ajax-completedata-ajax-failuredata-ajax-successSee Ajax Request Using Inline Attributes for some samples
Or you can call $.ajax from inside <scripts> tag:
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$( this ).addClass( "done" );
});
See jQuery.ajax() for more details.
You can use https://github.com/mjebrahimi/AspNetCore.Unobtrusive.Ajax
Install nuget package called: AspNetCore.Unobtrusive.Ajax
In startup in ConfigureServices add the following line services.AddUnobtrusiveAjax(useCdn: true, injectScriptIfNeeded: false);
In razor page place @Html.RenderUnobtrusiveAjaxScript() at the end of body and after jquery
Example of using instead of Ajax.BeginForm call:
using (Html.AjaxBeginForm("SendComment",
new AjaxOptions
{
HttpMethod = "Post",
InsertionMode = InsertionMode.InsertAfter,
UpdateTargetId = $"comments{Model.ExerciseId}",
OnBegin = "ExerciseScripts.toggleLoading($(this), $(this));",
OnSuccess = $"ExerciseScripts.toggleLoading($(this), $(this)); $('#textarea{Model.ExerciseId}').val('');",
OnFailure = "Notice.showError();"
}))
{
@Html.AntiForgeryToken()
@Html.Hidden("exerciseId", Model.ExerciseId)
@Html.TextArea("Comment", "",
new {
id = $"textarea{Model.ExerciseId}",
@class = "form-control",
placeholder = "Write you comment",
required ="required"
})
<br />
<button type="submit" class="btn btn-primary">
<i class="fa fa-paper-plane-o fa-lg"></i>
<span>Send</span>
</button>
}