MVC5AJAX表单

MVC5AJAX表单,ajax,asp.net-mvc,unobtrusive-ajax,Ajax,Asp.net Mvc,Unobtrusive Ajax,我正在使用Ajax.BeginFormhelper。它工作得很好,但我有一个小问题 代码如下: <div class="row" id="pdiv"> @using (Ajax.BeginForm("SomeAction","SomeController",new AjaxOptions{UpdateTargetId="pdiv"})) { ..... } </div> @使用(Ajax.BeginForm(“SomeAction”、“SomeController”、

我正在使用
Ajax.BeginForm
helper。它工作得很好,但我有一个小问题

代码如下:

<div class="row" id="pdiv">
@using (Ajax.BeginForm("SomeAction","SomeController",new AjaxOptions{UpdateTargetId="pdiv"}))
{
.....
}
</div>

@使用(Ajax.BeginForm(“SomeAction”、“SomeController”、新的AjaxOptions{UpdateTargetId=“pdiv”}))
{
.....
}
我已将UpdateTargetId设置为“pid”

因此,最初呈现的HTML如下所示:

<div class="row" id="pdiv">
<form action="/SomeController/SomeAction" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#pdiv" id="form0" method="post">
....

....
现在,当我进行提交时,它会将整个pid div放在pid中。i、 e

<div class="row" id="pdiv">
    <div class="row" id="pdiv">
    <form action="/SomeController/SomeAction" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#pdiv" id="form0" method="post">
    .....
    }

.....
}
我尝试设置Ajax选项
InsertionMode=InsertionMode.Replace

但是没有成功。还有什么方法可以让我用新获得的内容来替换“pdiv”吗?

你需要将你的ajax表单标记移动到一个单独的视图中,这样我就不会有你在目标id中指定的div元素,然后你应该从你的ajax操作中提供这个新的部分视图。

根据

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

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;
        }
    });
}
函数asyncOnSuccess(元素、数据、内容类型){
var模式;
如果(contentType.indexOf(“application/x-javascript”)!=-1{//jQuery已经为我们执行了javascript
返回;
}
mode=(element.getAttribute(“数据ajax模式”)| |“”).toUpperCase();
$(element.getAttribute(“数据ajax更新”))。每个(函数(i,更新){
var-top;
开关(模式){
“之前”一案:
top=update.firstChild;
$(“”).html(数据).contents().each(函数)(){
update.insertBefore(此,顶部);
});
打破
“之后”一案:
$(“”).html(数据).contents().each(函数)(){
update.appendChild(本);
});
打破
违约:
//由于生成重复的ID,更改了此行
//$(更新).html(数据);
$(update.html($(data.html());
打破
}
});
}

正如您在默认部分中所看到的,答案并没有替换updatetargetid,而是将其内容替换为答案。现在我得到了答案的内部部分,一切都很好

我也在做同样的事。但我认为应该有一个简单的短方法。这比创建局部视图容易吗?