如何使用onFailure
和OnSuccess
方法。目前我添加onFailure js方法,但它不会执行。也许我忘了添加一些脚本。 (我只添加了jquery.unobtrusive.ajax.js
)。
我必须在模态窗口中返回onFailure
partialview。
控制器中的代码
[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
答案 0 :(得分:1)
尝试这些小事, 添加这些脚本`
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.validate.js")"></script>`
并在document.ready
中 $(document).ready(function () {
$.validator.unobtrusive.parse('#form');
});
和表单
@using (Ajax.BeginForm("Controller Method", "Controller Name", new AjaxOptions { HttpMethod = "POST", OnSuccess = "JsonRequestForPassword_OnSuccess", UpdateTargetId = "forgotPassword" }, new { @id = "form" }))
更新目标ID应该是具有您的表单的任何div或占位符