如何使用onFailure
和OnSuccess
方法。目前我添加onFailure js方法,但它沒有執行。也許我忘了添加一些腳本。 (我只加了jquery.unobtrusive.ajax.js
)。我不得不在模態窗口中返回onFailure
partialview。Ajax.BeginForm及其方法onFailure和onSuccess
守則控制器
[HttpPost]
public ActionResult RequestPassword(RequestForPasswordViewModel passwordRequestViewModel)
{
if (!ModelState.IsValid)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return PartialView("RequestForPassword", passwordRequestViewModel);
}
代碼在局部視圖
@model YouCapital.Web.Models.ViewModels.RequestForPasswordViewModel
<div>
@using (Ajax.BeginForm("RequestPassword", FormMethod.Post, new AjaxOptions()
{
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
UpdateTargetId = "forgotPassword",
OnFailure = "JsonRequestForPassword_OnFailure",
OnSuccess = "JsonRequestForPassword_OnSuccess"
}, new { @class = "form-inline" }))
{
<fieldset>
<legend>Recover password</legend>
<div class="form-group">
@Html.Label("Enter your email")
@Html.TextBoxFor(x => x.Email)
</div>
<input type="submit" class="btn btn-default" value="Recover"/>
</fieldset>
@Html.ValidationSummary()
}
</div>
@section scripts
{
<script>
function JsonRequestForPassword_OnFailure() {
console.log('fail');
}
function JsonRequestForPassword_OnSuccess() {
console.log('success');
}
</script>
}
順便說一句,我不知道,如果它出了問題,但它是在引導modal
並作爲tabpanel
可能重複的問題HTTP:/ /stackoverflow.com/questions/5517813/how-to-use-ajax-beginform-onsuccess-and-onfailure-methods – Oleh 2014-10-30 11:44:57
顯示onFailure回調的代碼。和將觸發它的控制器代碼。 – 2014-10-30 11:52:37
我已經看到這個帖子,我已經腳本的方法,必須執行onFailure。但它的返回值只有PartialView(全屏,而不是模態),並且不執行javascript方法。我在其他帖子中添加了腳本,但不起作用。 – 2014-10-30 11:54:51