It is necessary to realize the possibility of increasing / decreasing / changing products in the basket using Ajax (without refreshing the page)

I have a method in the controller:

[HttpPost] public ActionResult IncreaseProductCount(int id) { cartService.IncrementCount(id); return RedirectToAction("Cart"); } 

CartService handles the search for a product and its increase.

In the "Cart" view there is such a markup for each item in the cart:

 <!--Кнопка минус--> <a href="#" class="btn btn-warning item-minus" data-product="@product.Product.Id"> <span class="fa fa-minus"></span> </a> <!--Поле с количеством--> <input type="text" size="2" value="@product.Count" data-product="@product.Product.Id" class="product-count form-control"/> <!--Кнопка плюс--> <a href="#" class="btn btn-danger item-plus" id="" data-product="@product.Product.Id"> <span class="fa fa-plus"></span> </a> 

In @section scripts {} there is such a script (for the + button)

  $('.item-plus').click(function(e) { e.preventDefault(); $.ajax({ type: 'POST', url: '@Url.Action("IncreaseProductCount", "Production")', data: { 'id': $(this).attr('data-product') }, success: function(result) { $('body').html(result); } }); }); 

Problem: using RedirectToRouteResult returns the markup of the passed view completely , so I used $ (' body ') .html (result). Everything seems to work with this, but only with each click is the "beard" of classes in the html class="....."> , which are copied with each click. With each click the page more and more "slows down".

No matter how logical it is to transfer the entire page every time, but in addition to the values ​​in the submission, I need to update the total number of goods in the header.

Tell me how to do it :)

  • If the Ajax method is used, the IncreaseProductCount method should simply return the result of the operation. Or the total number of items in the cart, for example. - koks_rs
  • But in my method InreaceProductCount, the method of the CartService service "IncrementCount" is called, which increases the quantity of the product in the basket stored in the session. Thus, I need to update the number of items in the Cart view and update the total number in the header of the page (or rather, in the partial view _ItemsInCart, which is used in _Layout). Those. it would be right, not to return anything, but to update all the items in the basket and their total number - Alexander Sankov

0