I am very new to MVC . So , have been studying the code of MusicStore Application in CodePlex.
I am unable to understand what does the following code meant:
// AJAX: /ShoppingCart/RemoveFromCart/5
[HttpPost]
public ActionResult RemoveFromCart(int id)
{
// Remove the item from the cart
var cart = ShoppingCart.GetCart(this.HttpContext);
// Get the name of the album to display confirmation
string albumName = storeDB.Carts
.Single(item => item.RecordId == id).Album.Title;
// Remove from cart
int itemCount = cart.RemoveFromCart(id);
// Display the confirmation message
var results = new ShoppingCartRemoveViewModel
{
Message = Server.HtmlEncode(albumName) +
" has been removed from your shopping cart.",
CartTotal = cart.GetTotal(),
CartCount = cart.GetCount(),
ItemCount = itemCount,
DeleteId = id
};
return Json(results);
}
//
// GET: /ShoppingCart/CartSummary
[ChildActionOnly]
public ActionResult CartSummary()
{
var cart = ShoppingCart.GetCart(this.HttpContext);
ViewData["CartCount"] = cart.GetCount();
return PartialView("CartSummary");
}
}
}
Please help me clarify , how that particluar HttpPost works as a Ajax Post back .
I know this is an old question but I came across it in my searches and to get this to work I had to add quotes around the Url.Action.
$.ajax({
type: "POST",
url: "@(Url.Action("RemoveFromCart"))",
data: ({
Id:1
}),
success: success,
dataType: dataType
})
Hi you can use jQuery Ajax to make an asynchronous postback request
please see the code below
$.ajax({
type: "POST",
url: @Url.Action("RemoveFromCart"),
data: ({
Id:1
}),
success: success,
dataType: dataType
})
[HttpPost] -- attribute ensures that RemoveFromCart action method accepts only post requests
[ChildActionOnly] -- attribute ensures that an action method can be called only as a child method from within a view. These are typically associated with partial views.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.