I have a form which I am trying to submit with Ajax / without reloading the page.
The Ajax call is hitting the server, but it doesn't seem to be calling the function. I've tried both $.post
and $.ajax
. Neither seem to work. I had something extremely similar working a few weeks back, but now I can't replicate it. (The end goal is to return the model as a partial view, in order to see the validation.)
Using Firefox, the controller method is immediately hit. Using Chrome, the browser tab seems to lock up. It sometimes hits the controller after a delay. (The model information is coming over accurately.)
What am I doing wrong here?
I have my partial view here:
<% using (Ajax.BeginForm("CreateSimpleReport", "Main", new AjaxOptions { HttpMethod="POST" }, new { id="CreateSimpleReport" })){ %>
<%: Html.ValidationSummary(false, "Report creation was unsuccessful. Please correct the errors and try again.", new { @class = "validation_summary" })%>
<% Html.EnableClientValidation(); %>
<div class="col">
<div class="row">
<div class="label">
<%: Html.LabelFor(m => m.Name)%>
</div>
<div class="field">
<%: Html.TextBoxFor(m => m.Name, new { @class = "input texbox" })%>
<%: Html.ValidationMessageFor(m => m.Name, "*")%>
</div>
</div>
<div class="row">
<div class="label">
<%: Html.LabelFor(m => m.Description)%>
</div>
<div class="field">
<%: Html.TextBoxFor(m => m.Description, new { @class = "input texbox" })%>
<%: Html.ValidationMessageFor(m => m.Description, "*")%>
</div>
</div>
<div class="row">
<div class="label">
<%: Html.LabelFor(m => m.Quantity)%>
</div>
<div class="field">
<%: Html.TextBoxFor(m => m.Quantity, new { @class = "input texbox" })%>
<%: Html.ValidationMessageFor(m => m.Quantity, "*")%>
</div>
</div>
<div class="right"><input type="submit" class="button" value="Create Report" /></div>
</div>
<% } %>
Controller:
[HttpPost]
public string CreateSimpleReport(SimpleReportModel model)
{
if (ModelState.IsValid)
{
// do something
return "success";
}
else
{
return "failure";
}
}
And finally, the jQuery:
$('#CreateSimpleReport').on('submit', function (e) {
var $this = $(this);
$.post((this), (this), function(data) {
alert('finish');
});
// $.ajax({
// type: 'POST',
// url: (this),
// data: (this),
// success: function () {
// alert('success');
// },
// error: function (jqXHR, textStatus, errorThrown) {
// alert('error');
// }
// }); // end ajax call
});