【问题标题】:desperately need help: Ajax.beginform calling twice, the second one with previous value急需帮助:Ajax.beginform 调用两次,第二次调用之前的值
【发布时间】:2018-09-24 17:48:38
【问题描述】:

所以,经过大量调试后,我知道在我的应用程序中包含一个带有几个复选框和一个提交按钮的局部视图,控制器/JS 被调用了两次。 这是转折点:

  1. 当 JS 被调用两次时。在第一个请求中,它保存复选框的正确值,然后在下一个请求中保存以前可用的值。
  2. 此调用不会始终如一地发生,它是随机发生的。还检查了数据或代码是否存在任何问题,但没有问题。所以问题是在 AJAX 调用本身产生的,但我无法找到原因。

谁能帮我解决这个问题。我真的很绝望。 我的代码如下:

我的部分观点:

using (Ajax.BeginForm("action", "controller", new AjaxOptions { OnComplete = "TimeLineObject.OnAjaxComplete", OnSuccess = "", OnBegin = "TimeLineObject.OnAjaxBegin", UpdateTargetId = "divEditorContent", OnFailure = "insertfailure" }, new { @class = "form-inline", id = "searchForm" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(m =>m.IsSave)
    <table class="table table-responsive">
    <tr>
        <th>#</th>
        <th>Document </th>
        <th>Input Type</th>
    </tr>
    @for (int i = 0; i < Model.model.Count; i++)
    {
        <tr>
            <td>
                @Html.HiddenFor(m => Model.model[i].Id)
                @Html.HiddenFor(m => Model.model[i].Id)
                @Html.CheckBoxFor(m => Model.model[i].IsChecked, new { @class = "form-control" })

            </td>
            <td>
                @Model.model[i].DocumentName
            </td>
            <td>
                @Model.model[i].InputType
            </td>
        </tr>
    }
 </table>
}

我的控制器:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    [ValidateInput(true), ValidateAntiForgeryToken]
    public ActionResult action(object documents)
    {
        using (CaseConfigManager manager = new CaseConfigManager())
        {
            var response = manager.layer2(documents.data,  SessionId);

            if (response.Status)
            {
                return this.PartialView("_Message", new MessageModel() { Type = MessageType.Success, Message = response.Message, Code = ClientStatussCode.TimeLine });
            }
            else
            {
                return this.PartialView("_Message", new MessageModel() { Type = MessageType.Warning, Message = response.Message, Code = ClientStatussCode.TimeLine });
            }
        }
    }

现在保存按钮位于生成部分视图的时间轴页面中。

时间线 JS:

OnAjaxBegin: function () {
    debugger;
    $('#formsave').attr('disabled', true);
},
OnAjaxComplete: function () {
    debugger;
    $('#formsave').attr('disabled', false);
    RegisterAutoCompute();
    RegisterValidation();
    TimeLineObject.RegisterDatePicker();
},
OnAjaxSuccess: function (HTML, Message) {
    TimeLineObject.FillHtmlContent(HTML);
    TimeLineObject.OnCloseClick();
    TimeLineObject.Message(Message);
},
OnAjaxFailure: function (error) {
    TimeLineObject.Message(TimeLineObject.Alerts.AjaxFail);
},

上面的 OnAjaxBegin 被调用了两次。

【问题讨论】:

    标签: javascript jquery ajax asp.net-mvc


    【解决方案1】:

    从这里开始,被调用一次

    public class HomeController : Controller
    {
        [HttpPost]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        [ValidateInput(true), ValidateAntiForgeryToken]
        public PartialViewResult IndexValid100(MyViewModel model)
        {
            return PartialView("_APartialViewWithJS");
        }
    
        public ActionResult IndexValid100()
        {
            return View(new MyViewModel());
    
        }
    

    _APartialViewWithJS.cshtml

    A Partial View 
    

    查看

    <!DOCTYPE html>
    <html>
    <head>
        <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
        @*!!!!You need the unotrusive-ajax.min.js*@
        <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jquery-ajax-unobtrusive@3.2.4/jquery.unobtrusive-ajax.min.js"></script>
        <script type="text/javascript">
                function onAjaxBegin() {
                    alert("ap");
                }
         </script>
    </head>
    <body>
        @using (Ajax.BeginForm(new AjaxOptions
        {
                 UpdateTargetId = "divEditorContent",
                 OnBegin = "onAjaxBegin"
        }
        ))
        {
            <input type="submit" value="Click Me" />
            <table class="table table-responsive">
                <tr>
                    <th>#</th>
                    <th>Document </th>
                    <th>Input Type</th>
                </tr>
            </table>
        }
        <div id="divEditorContent"></div>
    </body>
    </html>
    

    【讨论】:

    • hi @kblau 正如建议的那样,我已经添加了 httppost 属性,并且我在布局页面中有 jquery.unobtrusive-ajax.min.js
    • 嗨@kblau 它仍然无法正常工作。我观察到 AJAX 被调用了两次。
    • @TejaswiPandava 你在做什么和我不一样,这会导致 onajaxbegin 被调用两次?
    • 所以在我的设置中,以下内容有所不同: 1. jquery.unobtrusive-ajax.min.js 文件位于布局页面中。 2.按钮“”在时间轴视图中除了这些没有其他区别.
    • @Tejaswi Pandava 你想在按钮上有一个 onlick 事件吗?您是否认为只使用 AjaxOptions 的 OnBegin?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多