I am currently working on a project and trying to make a call to a controller function without triggering a page refresh. I have attempted setting up an ajax function for this purpose, but unfortunately, it has not been successful so far. I have tried placing the related script in both the HTML file and a separate jQuery.js file, but it seems like the form submission is not hitting the script at all. Any assistance on this matter would be highly appreciated! The form is located in a cshtml file, the controller in .cs, and the script in .js
The form in discussion:
<form id="tableForm" action="@(Url.Action("AddToOrder", "Order"))" method="post">
@{foreach (var item in (IList<Item>)ViewData["items"]){
<input type="hidden" id="@("item_id_" + item.id)" name="@("item_id_" + item.id)" value="@item.id" />
<tr>
<td>@item.description</td>
<td>@String.Format("{0:C}",item.price)</td>
<td><input type="text" id="@("item_quantity_" + @item.id)" name="@("item_quantity_" + @item.id)" style="width: 50px;" value="0" /></td>
</tr>
}}
<tr>
<td><input class="submit" type="submit" value="Add item(s) to order" /></td>
</tr>
</form>
The script causing issues:
$('#tableForm').submit(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
cache: false,
url: $(this).attr('action'),
data: $(this).serialize(),
succes: function (data) {
alert(data);
},
error: function (jXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
In summary, although the controller and method are functioning properly, the form submission results in a page refresh.