jQuery 如何从MVC中的主视图内部刷新/重新加载部分视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4409881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to refresh/reload partial view from inside main view in MVC
提问by Alex
I am displaying a partial view (shopping cart) inside my main (pricing view) view using RenderAction
我正在使用 RenderAction 在我的主(定价视图)视图中显示一个局部视图(购物车)
<% Html.RenderAction("Cart", "ShoppingCart"); %>
As user adds items to cart on the main view I have to update the partial view to display newely added items. Here is the code for my partail view:
当用户在主视图上将商品添加到购物车时,我必须更新部分视图以显示新添加的商品。这是我的部分视图的代码:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Trl.ViewModels.Cart>" %>
<% foreach (var _hotel in Model.Hotels)
{ %>
Hotel Name: <%: _hotel.Name %> <br/>
Price: <%: _hotel.TotalPrice %>
<% } %>
"Trl.ViewModels.Cart" object used in above user control will contain newely added items as I am using ASP.NET Session State to persist Cart. How to refresh/reload this partial view from my main view so it can display newely added items?
上面用户控件中使用的“Trl.ViewModels.Cart”对象将包含新添加的项目,因为我正在使用 ASP.NET 会话状态来持久化购物车。如何从我的主视图刷新/重新加载这个局部视图,以便它可以显示新添加的项目?
回答by jim tollan
ok, i've got a clear(er) idea of whats required.
好的,我对需要什么有一个清晰的(呃)想法。
I'm assuming that the main content area is updated via ajax. If this is the case, then my approach would be to make the update via ajax and in the controller action, return partialview that contains the sidebar cart. For this to work, you'd need a named id for the div that contained the cart in order to target that as an update site.
我假设主要内容区域是通过 ajax 更新的。如果是这种情况,那么我的方法是通过 ajax 进行更新,并在控制器操作中返回包含侧边栏购物车的部分视图。为此,您需要为包含购物车的 div 指定一个命名 id,以便将其定位为更新站点。
without further ado, a quick code demo of what i mean:
不用多说,我的意思的快速代码演示:
controller:
控制器:
public ActionResult Create(Cart item)
{
if (ModelState.IsValid)
{
// or could be added to session["cart"] etc..
_repository.Add(item);
var cartItems = _repository.Find(x => x.cartID = item.cartID);
return PartialView("CartMini", cartItems);
}
return new EmptyResult();
}
main view (in your right hand column - this would be where your partial view was originally defined):
主视图(在您的右手栏中 - 这将是您最初定义局部视图的地方):
<div id="miniCart"></div>
in another part of the page (semi psuedo-code):
在页面的另一部分(半伪代码):
<script type="text/javascript">
function updateCart() {
var tdata = $(frm).serialize();
// or your data in the format that will be used ??
$.ajax({
type: "POST",
data: tdata,
url : '<%= Url.Action("Create", "Cart") %>',
dataType: "json",
success: function (result) { success(result); }
});
});
function success(result){
$("#miniCart").html(result);
}
</script>
and that really is the bare bones of what's needed. sorry for the rush on this, had started a while ago and phone went, now have to head off :). will add further if not clear later.
这确实是所需要的最基本的东西。很抱歉这么匆忙,不久前就开始了,电话响了,现在必须离开:)。后面不清楚的话再补充。