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 :)