【问题标题】:ajax.beginform - OnFailure dont return partial viewajax.beginform - OnFailure 不返回部分视图
【发布时间】:2020-04-27 15:04:35
【问题描述】:

你好

我正在使用 ajax.beginform,我想用 里面的错误。当我将状态代码更改为不好的触发时 OnFailure 方法不返回部分视图。调用的视图:

<fieldset>
@using (Ajax.BeginForm("SaveSocioDetails", "Student", new AjaxOptions { HttpMethod = "POST", OnSuccess = "firstsuccess",OnFailure = "sociodetailsfail", UpdateTargetId="partialsocio" ,LoadingElementId = "div_loading" }, new { @enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
<div id="partialsocio">
    @Html.Action("PartialSocioDetails", "Student", new { SpId = ViewBag.SpId })
</div>
        <div id="div_loading" style="display:none;">
        <img src="@Url.Content("~/Content/Pic/Spinner.gif")" alt="" />
    </div>
    <button class="btn btn-primary" type="submit" value="Submit">שלח</button>
}
<input type="button" name="next" class="next action-button" value="Next" />

我的控制器:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SaveSocioDetails(SpSocio socio) // ajax for 1 step in socio
        {
            socio.StudentId = sStudentId; // bind student id to socio model
            bool sociook = SocioDetValid(socio);
            // add validation
            if (ModelState.IsValid && sociook)
            {
                SaveSocioModel(socio);
                Response.StatusCode = 200;
            }
            else
               Response.StatusCode = 300; // return error to client the model is not valid
            return PartialView("~/Views/Student/Socio/SocioDetails.cshtml", socio); // return the partial view of the forn with validation messages
        }

js:

<script>
   $(document).ready(function ()
    {
    });

    function firstsuccess(data) {
        console.log(data);
        $('#partialsocio').html(data);
        console.log('this is ajaxSuccess');
    }

    function sociodetailsfail(bdata) {
         console.log('this is ajaxfail');
        console.log(data);
        $('#partialsocio').html(data);
    }
</script>

请帮我解决这个问题

【问题讨论】:

  • 您的页面中有jquery.unobtrusive-ajax.js 的引用吗?
  • 是的,我已经添加了这个参考
  • 参考文献的顺序是什么?你能告诉我们你是如何引用它们的吗?
  • 我遇到了问题,文本在返回的代码中

标签: c# asp.net-mvc model-view-controller asp.net-ajax ajax.beginform


【解决方案1】:

如果您的请求失败,那么您将从服务器获得回调,包括sociodetailsfailwitin java-script 中的问题定义,您可以在其中放置逻辑以显示您从服务器收到的错误消息bdata 反对用户

【讨论】:

  • 但是 bdata 没有返回我想在视图中重新加载的部分视图。他有错误代码,里面没有部分视图
猜你喜欢
  • 2012-09-06
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
相关资源
最近更新 更多