MVC Ajax.BeginForm替换奇怪的行为 [英] MVC Ajax.BeginForm Replace strange behaviour

查看:94
本文介绍了MVC Ajax.BeginForm替换奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在部分视图中,我正在使用MVC Ajax.Beginform,如下所示:

In a partial view, I am using MVCs Ajax.Beginform like followed:

<div id="divToReplace">
    @using (Ajax.BeginForm("Action", "Controller,
                           new AjaxOptions
                           {
                               InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
                               UpdateTargetId = "divToReplace"
                           },
                           new
                           {
                                id = "formID"
                           }))
    {
        ...
</div>

提交表格时,我希望孔div divToReplace被替换为答案(再次是局部视图),但是div的内部html divToReplace被替换为答案,因此局部视图的开始看起来像这样:

When submitting the form, I would expect that the hole div "divToReplace" is replaced by the answer (the partial view again). But instead the inner html of the div "divToReplace" is replaced by the answer, so the beginning of the partial view looks like this:

<div id="divToReplace">
    <div id="divToReplace">
           ...

我在做什么wr

推荐答案

嗯,经过一段时间,我遇到了同样的问题,现在我想弄清楚,所以看看jquery.unobtrusive-ajax.js和负责的函数:

Well, after a certain time, I ran into the same problem and now I wanted to make it clear so I had a look in jquery.unobtrusive-ajax.js and the responsable function:

function asyncOnSuccess(element, data, contentType) {
    var mode;

    if (contentType.indexOf("application/x-javascript") !== -1) {  // jQuery already executes JavaScript for us
        return;
    }

    mode = (element.getAttribute("data-ajax-mode") || "").toUpperCase();
    $(element.getAttribute("data-ajax-update")).each(function (i, update) {
        var top;
        switch (mode) {
            case "BEFORE":
                top = update.firstChild;
                $("<div />").html(data).contents().each(function () {
                    update.insertBefore(this, top);
                });
                break;
            case "AFTER":
                $("<div />").html(data).contents().each(function () {
                    update.appendChild(this);
                });
                break;
            default:
                // Changed this line because of generating duplicate IDs
                //$(update).html(data);
                $(update).html($(data).html());
                break;
        }
    });
}

如您在默认部分中所见,答案不是替换updatetargetid但将其内容替换为答案。现在,我回答了问题的内在部分,一切正常!

As you can see in the default part, the answer was not replacing the updatetargetid but replaced its content with the answer. Now I take the inner part of the answer and everything works fine!

这篇关于MVC Ajax.BeginForm替换奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆