拖放不放入购物车
我可以拖动专辑封面的图像,当它们从购物车按钮上掉落时,它们会克隆并返回到原来的位置,但是当我将其放在购物车按钮上时,它不会更新购物车,它只会返回到原来的位置状态。为什么会出现这种情况?
$("#droppable").droppable({
drop: function (event, ui) {
var AlbumToAdd = ui.draggable.data("id");
if (AlbumToAdd != '') {
// Perform the ajax post
$.post("/ShoppingCart/DragToCart", { "id": AlbumToAdd },
function (data) {
// Successful requests get here
// Update the page elements
$('#cart-status').text("Cart (" + data.CartCount + ")");
});
}
}
});
控制器评论
//
// GET: /Store/DragToCart/5
public ActionResult DragToCart(int id)
{
// Retrieve the album from the database
var addedAlbum = storeDB.Albums
.Single(album => album.AlbumId == id);
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(addedAlbum);
var results = new DragToCartViewModel
{
Message = Server.HtmlEncode(addedAlbum.Title) +
"Your cart has been updated",
CartTotal = cart.GetTotal(),
CartCount = cart.GetCount(),
AddedId = id
};
return Json(results);
如果您想查看更多代码,请
I can drag the images of album art and they clone and go back to their original place when dropped away from the cart button, but when I drop it on the cart button it doesn't update the cart, it just goes back to its original state. Why is this occuring?
$("#droppable").droppable({
drop: function (event, ui) {
var AlbumToAdd = ui.draggable.data("id");
if (AlbumToAdd != '') {
// Perform the ajax post
$.post("/ShoppingCart/DragToCart", { "id": AlbumToAdd },
function (data) {
// Successful requests get here
// Update the page elements
$('#cart-status').text("Cart (" + data.CartCount + ")");
});
}
}
});
Controller
//
// GET: /Store/DragToCart/5
public ActionResult DragToCart(int id)
{
// Retrieve the album from the database
var addedAlbum = storeDB.Albums
.Single(album => album.AlbumId == id);
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(addedAlbum);
var results = new DragToCartViewModel
{
Message = Server.HtmlEncode(addedAlbum.Title) +
"Your cart has been updated",
CartTotal = cart.GetTotal(),
CartCount = cart.GetCount(),
AddedId = id
};
return Json(results);
Comment if you want to see more code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否发现哪部分代码不起作用?是可放置的JS还是控制器?如果你把alert(“blah”);在你的 JS 中你可以找出哪些代码没有被触发。
Have you found out which part of the code doesn't work? Is it the droppable JS or the controller? If you put alert("blah"); in your JS you can find out which code doesn't get triggered.