【问题标题】:Ajax call to refresh a partialview from a controller从控制器刷新局部视图的 Ajax 调用
【发布时间】:2018-06-10 01:23:40
【问题描述】:

我的控制器中有以下功能:

 public ActionResult DraftOrderDetailsLineItems (Guid customerId)
    {
        //lookup cart based on customer ID
        var draftOrder = new ComApiCart(customerId);

        return PartialView("_draftOrderDetails",draftOrder.OrderDetails);
    }

在我看来,我有部分代码

  <tbody id="draftOrderItems">
     @Html.Partial("_draftOrderDetails", Model.Order);
  </tbody>

现在我需要有关 Ajax 调用的帮助。当单击表格中的 tr 以将此项目添加到购物车时,首先调用此调用。这很好用,我只需要在成功重新加载视图上的 _draftOrderDetails 部分视图后得到一些帮助。

$(document).on('click', '#search-results tr', function (event) {
//Add to cart
var id = $(this).find('#SKU').val();
var userId = $('#ID').val();
$.ajax({
    url: "/orders/addtocart",
    type: 'POST',
    cache: false,
    dataType: 'json',
    data: { "productId": id, "Quantity": 1, "UserID": userID, "Description": "" },
    success: function (data) {

        //ajax call to DraftOrderDetailsLineItems to refresh the lines 

    },
    error: function (jqXHR, textStatus, errorThrown) {
        productQty.val(0);
        console.error("[Error in Ajax Request, Add To Cart] Code:" + jqXHR.status + " Error:" + errorThrown + " \nText Status:" + jqXHR.responseText);
        timeoutID = window.setTimeout(updateCurrentSession(ACTION.UPDATE), timeoutDelay);
    }
});

});

我知道我在 data.id 字段中有来自 Ajax 调用的客户 ID,我只是在努力使调用本身在将项目添加到购物车后“重新呈现”部分视图。

谢谢。

【问题讨论】:

    标签: asp.net ajax asp.net-mvc asp.net-ajax partial-views


    【解决方案1】:

    您可以像下面这样更改成功部分,一切都会好起来的。

    success: function (data) {
    
        //ajax call to DraftOrderDetailsLineItems to refresh the lines 
    			$.get('/YourController/DraftOrderDetailsLineItems/',{customerId:data.id},function(data){
    					$("#draftOrderItems").html(data);
    			})
    },

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2014-03-27
      相关资源
      最近更新 更多