我的MVC应用程序中有一个ajax表单,根据表单的结果指定了两个选项。这些选择是成功的'并且'失败'这些选项中的每一个都按如下方式调用相应的函数:
function Success() {
...all good...
}
function Failure() {
..that didn't work...
}
在我的控制器中,有一个条件是,如果数据库中已存在某个项目,则应返回500内部服务器错误的状态码。
这是我的所有代码。
查看
@using (Ajax.BeginForm("Create", "Offers", new AjaxOptions
{
OnSuccess = "Success",
OnFailure = "Failure"
}))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.requirement_idx);
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
...form goes here
}
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(OfferViewModel vm)
{
bool exists = db.tbl_requirement_vessel_offer
.Where(c => c.requirement_idx == requirement_idx)
.Any(o => o.vessel_idx == vessel_idx);
if (exists)
{
return new HttpStatusCodeResult(500, "Already added");
}
else
{
if (ModelState.IsValid)
{
..Do magic and save...
}
}
return View(vm);
}
上面已经提到了成功和失败选项的javascript。无论如何我可以在我的ajax表单的失败函数中捕获这个返回的状态代码吗?