我使用jQuery来POST
我的表单到控制器和控制器函数总是在return RedirectToAction("Index")
但它没有重定向。
我的jQuery代码:
<script type="text/javascript">
$("form").on("submit", function (e) {
e.preventDefault();
if ($('form').valid()) {
var f = $("form");
var dialog = bootbox.dialog({
message: '<p><i class="fa fa-spinner fa-spin"></i> Proszę czekać...</p>',
size: 'small',
closeButton: false
});
$.post(f.action, f.serialize(), function (result) {
console.log(result);
dialog.modal('hide');
});
return false;
}
});
</script>
我的示例控制器功能代码:
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
InvoiceTransfer invoiceTransfer = db.InvoiceTransfers.Find(id);
int pwID = Convert.ToInt32(System.Web.HttpContext.Current.Session["PaintballWorkerID"]);
if (invoiceTransfer.FromID != pwID)
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
for (int i = 0; i < invoiceTransfer.Invoices.Count; i++)
{
db.InvoiceTransfers.Include("Invoices").FirstOrDefault(z => z.InvoiceTransferID == id).Invoices.Remove(invoiceTransfer.Invoices[i]);
}
db.InvoiceTransfers.Remove(invoiceTransfer);
db.SaveChanges();
return RedirectToAction("Index");
}
它总是在函数末尾转到Index,但浏览器没有重定向。正如您所看到的,我使用console.log(result)
并且在控制台result
中始终是我的索引页面的html代码。