gpt4 book ai didi

jquery - Ajax.BeginForm 及其方法 onFailure 和 onSuccess

转载 作者:行者123 更新时间:2023-12-01 01:33:22 27 4
gpt4 key购买 nike

如何使用 onFailureOnSuccess 方法。目前我添加了 onFailure js 方法,但它不执行。也许我忘记添加一些脚本。 (我只添加了jquery.unobtrusive.ajax.js)。

我必须在模态窗口中返回 onFailure 部分 View 。

Controller 中的代码

  [HttpPost]
public ActionResult RequestPassword(RequestForPasswordViewModel passwordRequestViewModel)
{
if (!ModelState.IsValid)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return PartialView("RequestForPassword", passwordRequestViewModel);
}

部分 View 中的代码

@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

最佳答案

尝试这些小事情,添加这些脚本`

<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 或占位符

关于jquery - Ajax.BeginForm 及其方法 onFailure 和 onSuccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26652005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com