如何使用onFailureOnSuccess方法。目前,我添加了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

最佳答案

试试这些小东西,
添加这些脚本

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

09-11 20:35