自AJAX的更新局部视图/控制器在ASP.Net MVC,复制的DIV视图、控制器、局部、AJAX

2023-09-10 13:26:20 作者:逍遥人生

我在MVC中的部分观点是这样:

I have a partial view in MVC that goes something like:

<div id="comments">
    ...
</div>

在该专区有使用AJAX调用一个控制器和回来相同的局部视图的形式。问题是,调用视图的结果替换分区,而不是整个div的内容,我结束了:

Inside that div there's a form that calls a controller using AJAX and gets back that same partial view. The problem is that the results of calling the view replaces the contents of the div, not the whole div, and I end up with:

<div id="comments">
    <div id="comments">
        ...
    </div>
</div>

唯一的解决办法我能想到的与我的ASP.Net MVC和AJAX的经验星期就是把DIV的局部视图外,使局部视图只包含内侧部分,但随后的形式将指向一个在视图,表单位于打破了小封装,我已经离开那里之外ID。有没有更好的解决办法?

The only solution I can think about with my week of experience in ASP.Net MVC and AJAX is to put the div outside the partial view and make the partial view only contain the inside part, but then the form would refer to an id outside the view where the form is located breaking the little encapsulation I have left there. Is there any better solution?

推荐答案

附带.NET MVC 3使用基于从jQuery.ajax四个回调回调不显眼的Ajax库。然而,InsertionMode = InsertionMode.Replace从Ajax.BeginForm方法做的没有的结果jQuery的replaceWith被调用。相反,html的(数据)被用来取代的内容的目标元素的(而不是元素本身)。

The unobtrusive Ajax library that ships with .NET MVC 3 uses callbacks that are based on the four callbacks from jQuery.ajax. However, the InsertionMode = InsertionMode.Replace from the Ajax.BeginForm method does not result in jQuery's replaceWith being called. Instead, .html(data) is used to replace the contents of the target element (and not the element itself).

我描述了一个解决方案,在我的博客这个问题: 的http://buildingwebapps.blogspot.com/2011/11/jquerys-replacewith-and-using-it-to.html

I have described a solution to this problem on my blog: http://buildingwebapps.blogspot.com/2011/11/jquerys-replacewith-and-using-it-to.html