J
J
JoraInTheSky2017-08-01 15:32:49
ASP.NET
JoraInTheSky, 2017-08-01 15:32:49

Two actions for one Ajax request?

There is a form for adding items to the cart,

@using (Ajax.BeginForm("AddCart", "Cart", new AjaxOptions { UpdateTargetId = "res" }))
            {
                <div class="pull-left">
                    @Html.HiddenFor(x => x.Id)
                    <input type="submit" class="btn btn-default" value="Добавить в корзину" />
                </div>
            }

the AddCart method adds a product to the cart and returns a partial view that updates the counter of the number of items in the cart
[HttpPost]
        public ActionResult AddCart(int Id)
        {
            AddProductToCart(id);
            return PartialView();
        }

Already in the basket, when all the products that are in it are displayed, a form is called that removes the product from the basket:
@using (Ajax.BeginForm("RemoveFromCart", "Cart", new AjaxOptions { UpdateTargetId = "tabcart" }))
                        {
                                @Html.Hidden("Id", line.Product.Id)
                                <input type="submit" class="btn btn-default" value="Удалить" />
                        }

The RemoveFromCart method removes an item from the cart and returns a new partial view of the item table.
[HttpPost]
        public ActionResult RemoveFromCart(int Id)
        {
            RemoveProductFromCart(id);
            return PartialView(new CartIndexViewModel
            {
                Cart = GetCart(),
                ReturnUrl = null
            }); 
        }


But the counter with the number of goods in the header is not updated, so the question is how can I update the contents of the basket and update the counter of the quantity of goods in the basket with one ajax action?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2017-08-01
@dmitryKovalskiy

You can update the cart status on onSuccess.
Note. You don't have a uniform method naming style. AddCart - add cart(???)
RemoveFromCart - Remove from cart.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question