如何将两个值添加到一个值中 [英] How to add two values into one value

查看:115
本文介绍了如何将两个值添加到一个值中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好任何人都可以用mycart的链接或代码来帮助我..我的问题是,当我点击产品页面中的一个对象时,它会进入我的购物车页面..但是我点击了多少次添加到购物车按钮..那个很多次它提交甚至认为它是相同的项目。我想把它改成像...当我点击添加到购物车按钮如果该项目已经存在于mycart中,它必须添加到现有项目(如果两者都相同)。如果没有,那么添加到购物车作为第二项。请它紧急分配PLZ帮助我....

Hello can any one help me with links or code for mycart.. My problem is that when i click a object in products page its going into my cart page.. but how many times im click add to cart button.. that many times its been submitting even thought its the same item. i want to change it into like.. when i click on add to cart button if that item is already exists in the mycart it has to get added to existing item(if both are same). if not then add to cart as 2nd item. please its an urgent assignment plz do help me....

推荐答案

你可以用这样的东西来做:

You can make it by something like this:
public void AddItem(int productId) {
        // Create a new item to add to the cart
        CartItem newItem = new CartItem(productId);

        // If this item already exists in our list of items, increase the quantity
        // Otherwise, add the new item to the list
        if (Items.Contains(newItem)) {
            foreach (CartItem item in Items) {
                if (item.Equals(newItem)) {
                    item.Quantity++;
                    return;
                }
            }
        } else {
            newItem.Quantity = 1;
            Items.Add(newItem);
        }
    }



参考文章在ASP.NET中构建购物车 [ ^ ]



另一种方式:

例如:


Refer article Build a Shopping Cart in ASP.NET[^]

Another way:
e.g:

public void AddToCart(Album album)
        {
            // Get the matching cart and album instances
            var cartItem = storeDB.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId
                && c.AlbumId == album.AlbumId);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                {
                    AlbumId = album.AlbumId,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                storeDB.Carts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart,
                // then add one to the quantity
                cartItem.Count++;
            }
            // Save changes
            storeDB.SaveChanges();
        }



找到它:这里 [ ^ ]


创建一个这样的私有方法。

Create a private method like this.
private List<MYCart> sessionOfCarts
{
    get
    {
        if (Session["sessionOfCarts"] == null)
            this.sessionOfCarts = new List<MYCart>();
        return (List<MYCart>)Session["sessionOfCarts"];
    }
    set
    {
        Session["sessionOfCarts"] = value;
    }
}





//主要方法......





//Main Method...

public void AddMyCart(MYCart selectedItem)
 {
     if(sessionOfCarts.Any(p =>p.cartId == selectedItem.cartId))
     {
         var sample = sessionOfCarts.Select(p =>{
                  if (p.cartId == selectedItem.cartId)
                  { p.Quantity = p.Quantity + 1; }; return p; }).ToList();
     }
     else
     {
       sessionOfCarts.Add(selectedItem);
     }

 }







你可以从sessionOfCarts获取选定的购物车..



完成此过程后清除该会话..




You can get the selected carts from sessionOfCarts..

Clear that Session once you complete this process..


这篇关于如何将两个值添加到一个值中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆