简体   繁体   English

ASP.NET MVC呈现不必要的多个局部视图

[英]ASP.NET MVC rendering unwanted multiple partial views

Question Background: 问题背景:

I've developed a simple 'cart' system that features a table populated with items from an assosicated controller method which has all the normal functionality you would expect from an eCommerce cart ie remove an item, update all items. 我已经开发了一个简单的“购物车”系统,该系统具有一个表格,该表格中填充了关联控制器方法中的项目,该方法具有您希望从电子商务购物车中获得的所有正常功能,即删除项目,更新所有项目。 The functionality all works but I'm having issues with the 'Update' partial views of the cart. 该功能全部正常,但是购物车的“更新”局部视图出现问题。

The Issue: 问题:

I currently have a partial view named _ViewCartContents.cshtml which renders the cart items table along with an 'Update Cart' button: 我目前有一个名为_ViewCartContents.cshtml的局部视图,该视图呈现了购物车项目表以及“更新购物车”按钮:

<table id="Table1" cellspacing="3">
 <tr>
    <td style="display:none;">id</td>
    <td></td>
    <td><b>Item</b></td>
    <td><b>Brand</b></td>
    <td><b>Unit Price</b></td>
    <td><b>Quantity</b></td>
    <td></td>
 </tr>
</table>
<br />
<input class="btn btn-success btn-block updateCart" type="button" value="Update Cart" />
  <div class="row">
    <h2>Total: £@ViewBag.Total</h2>
 </div>

'Update Cart' button JQuery of the _ViewCartContents.cshtml partial view along with the Ajax POST call to the UpdateAllCartItems method on the CartController . 所述的“更新购物车”按钮的JQuery _ViewCartContents.cshtml与Ajax的POST调用沿局部视图UpdateAllCartItems在方法CartController Upon a successful post, the partial view is sent back to show the table with its updated item quantity's: 成功发布后,会将部分视图发送回以显示带有更新项目数量的表:

<script type="text/javascript">

$(".updateCart").click(function () {

    $("tr.item").each(function () {

        var $prodId = $(this).find("td.prodId").html();
        var $prodQty = $(this).find("input").val();

        $.ajax({
            url: '@Url.Action("UpdateAllCartItems")',
            type: 'POST',
            data: {
                "id": $prodId,
                "qty": $prodQty
            },
            success: function (partialView) {
                $('#Table1').html(partialView);
            }
        });

    });
});

The main ViewCartContents.cshtml view that renders the partial view: 呈现部分视图的主ViewCartContents.cshtml视图:

<div class="container">
@Html.Partial("_ViewCartContents")
@Html.ActionLink("Proceed To Checkout", "ShippingDetails", "Checkout")

Cart Controller method UpdateAllCartItems : 购物车控制器方法UpdateAllCartItems

public ActionResult UpdateAllCartItems(string id, string qty)
    {
        \\Logic for updating cart

        return PartialView("_ViewCartContents", ViewBag);
    }

As shown the 'ViewCartContents.cshtml' view renders as expected showing the contents of the cart on the table and also displaying the single 'Update Cart' button along with a 'Total' amount - for this example I added a single item to the cart with a quantity of '1': 如图所示,“ ViewCartContents.cshtml”视图按预期方式呈现,在表上显示购物车的内容,还显示单个“更新购物车”按钮以及“总计”数量- 在本示例中,我向购物车添加了一个项目数量为“ 1”:

在此处输入图片说明

When I change the item quantity from 1 to 2 and then update the contents by clicking on the 'Update Cart' button the partial view is creating yet another copy of itself which obviously I don't want The newly displayed 'Total' value is showing the correct value of the cart content though. 当我将项目数量从1更改为2,然后通过单击“更新购物车”按钮更新内容时, 局部视图正在创建其自身的另一个副本,这显然是我不希望的 。显示了新显示的“总计”值购物车内容的正确值。

在此处输入图片说明

If anyone could tell me why the 'Update Cart' and 'Total' heading is re-rendering that would be great. 如果有人能告诉我为什么“更新购物车”和“总计”标题重新呈现,那将是很好的。

Your partial view renders and table and the button/total elements, yet you ajax call only replaces the contents of the table, leaving the original button/total elements. 您的局部视图渲染了表格和表格以及button / total元素,但您的ajax调用仅替换了表格的内容,而保留了原始的button / total元素。

Wrap the current elements in another container 将当前元素包装在另一个容器中

<div id="container">
  <table cellspacing="3">
    <tr>
      <td style="display:none;">id</td>
      ....
    </tr>
  </table>
  <br />
  <input class="btn btn-success btn-block updateCart" type="button" value="Update Cart" />
  ....
</div>

and adjust the ajax success call back to replace the contents of that container 并调整ajax成功回调以替换该容器的内容

....
success: function (partialView) {
  $('#container').html(partialView);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM