Hello everyone and thanks for the help in advance. I am developing an Asp.Net Core application that uses jQuery ajax to post data to a controller that returns a partial view to update the page. Since this is an ajax call, I don't want return the entire page, instead wanting only to update a small portion. I have attempted to override the button click:
$(document).ready(function () {
$('#btnupload').off('click').on('click', function (event) {
event.preventDefault();
let form = $("#frmupload");
let url = form.attr('action');
$.ajax({
type: "POST",
url: url,
data: form.serialize(), // Serialize form data
success: function (response) {
$("#divRESPONSE").html(response);
},
error: function (data) {
alert("Error occurred while submitting the form");
}
});
});
});
And the HTML
<form id="frmupload" name="frmupload" method="post" action="/Controller/Upload">
<input type="hidden" id="ID" name="ID" value="123" />
<input id="btnupload" name="btnupload" type="submit" value="Upload" />
</form>
When the button is clicked, the form posts to /Controller/Upload, but ends up displaying the returned partial view like a complete page.
The controller looks like:
[HttpPost]
public IActionResult Upload(int ID)
{
// do some database stuff
return PartialView("~/Views/Shared/_PartialView.cshtml");
}
Any help would be appreciated.