I have a shopping cart. I also have a partial view cartSummary – it is a simple partial View that lists all offers in cart.
Somewhere on my page I click “add to cart” Ajax.ActionLink, it adds the selected oofer to the cart as follows:
public ActionResult AddToCart(Cart cart, string offerId)
{
OffersServices offersServices = new OffersServices();
var offer = offersServices.GetOfferDetails(offerId);
if (offer != null && offer.Count > 0)
{
offersServices.GetImagesAndDescriptionForOffer(offer[0]);
cart.AddOffer(offer[0]);
}
return Content("Offer added to the cart");
}
Everything is working fine except that the cart summary is not updating itself. Therefore, what should be done in order to update the cart summary, after the “AddToCart” action is called?
You could return a partial view from your action that will point to the summary partial and pass it the required model so that it can update:
and then you will instruct your Ajax.ActionLink to update the summary using the
UpdateTargetIdproperty ofAjaxOptionsand pointing it to some div that will contain the summary. Now when the AJAX call succeeds the partial that was returned by your controller action will be injected into the DOM and replace the current summary.