如何控制数量和增加

时间:2014-06-04 07:47:25

标签: c# asp.net ajax web-services

我在这里使用webservice将产品添加到购物篮中的代码;

[WebMethod]

    public void calistir(int urunid, string GirenIp)
    {
        using (Models.DermabonEntities db = new Models.DermabonEntities())
        {


            var productName = (from i in db.Product
                               where i.Id == urunid
                               select i.ProductName).FirstOrDefault();
            var productPrice = (from i in db.Product
                                where i.Id == urunid
                                select i.ProductPrice).FirstOrDefault();

            var productId = (from i in db.Product
                             where i.Id == urunid
                             select i.Id).FirstOrDefault();
            var productPic = (from i in db.Product
                              where i.Id == urunid
                              select i.ProductPicture).FirstOrDefault();

            var userIp = GirenIp;

            Basket create = new Basket();

            create.ProductName = productName;
            create.ProductId = productId;
            create.ProductPrice = productPrice;
            create.ProductPic = productPic;
            create.UserId = userIp;
            var qua = "2";
            create.ProductQuantity = Convert.ToInt32(qua);


            db.Basket.Add(create);
            db.SaveChanges();




        }

    }

这里我手动添加产品数量。但我想控制是否有相同的产品增加数量,如果没有数量1。

我尝试过这样的事情;

var CountProduct= db.Basket.FirstOrDefault();
            if (CountProduct.ProductId == urunid)
            {
                CountProduct.ProductQuantity += Convert.ToInt32(adet);
                create.ProductQuantity = Convert.ToInt32(adet);
                db.SaveChanges();
            }

GirenIp =来自ajax的sessionID,来自ajax的urunId = ProductId,adet表示数量。

Ajax;

  function sepeteEkle(id) {

         var urunid = id;
         var GirenIp = $("#userId").val();
         $.ajax({
             dataType: "json",
             type: "POST",
             contentType: "application/json",
             url: "/Admin/WebService/Control.asmx/calistir",
             data: "{'urunid':'" + urunid + "','GirenIp':'" + GirenIp + "' }",
             success: function(){
                 $("#cartContent").load("MiniSepet.aspx #cartContent");
             },
             error: function () {
                 $("#cartContent").load("MiniSepet.aspx #cartContent");
             }
         });
         return false;
     }


     $(document).on('click', '.sepetat', function () {
         var nId = $(this).data("id")
         sepeteEkle(nId);
     });



sepetat= add to basket
篮子桌;

 public partial class Basket
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public Nullable<int> ProductId { get; set; }
    public string ProductName { get; set; }
    public Nullable<double> ProductPrice { get; set; }
    public Nullable<int> ProductQuantity { get; set; }
    public string ProductPic { get; set; }
}

}

1 个答案:

答案 0 :(得分:1)

您不希望在同一对象上运行多个LINQ查询,只是为了获得多个属性。这将导致SQL代码多次运行(在您的示例中为4次)。只需获取一次对象并使用其属性。

您还想稍微清理一下并使用这样的简单模式:

public void calistir(int urunid, string GirenIp) 
{
    using(Models.DermabonEntities db = new Models.DermabonEntities()) 
    {
        // Find the product by primary key
        var product = db.Product.Find(urunid);
        if (product != null) 
        {
            var productName = product.ProductName;
            var productPrice = product.ProductPrice;
            var productId = urunid;
            var productPic = product.ProductPicture;

            var userIp = GirenIp;

            // Get existing basket entry if any based on session/user id and product id
            Basket basket = dn.basket.FirstOrDefault(x=>x.UserId == userIp && x.ProductId == productId);
            if (basket == null) 
            {
                // basket does not already exist, so add new basket
                basket = new Basket() 
                {
                    // You can use property assignment with constructors
                    ProductName = productName,
                    ProductId = productId,
                    ProductPrice = productPrice,
                    ProductPic = productPic,
                    UserId = userIp,
                    ProductQuantity = 1      // Your initial quantity
                }
                db.Basket.Add(basket);
            } 
            else 
            {
                // Existing basket, just increase the quantity
                basket.ProductQuantity++;
            }
            db.SaveChanges();
        }
    }
}

实际上并不需要大多数临时变量:

public void calistir(int urunid, string GirenIp) 
{
    using(Models.DermabonEntities db = new Models.DermabonEntities()) 
    {
        // Find the product by primary key
        var product = db.Product.Find(urunid);
        if (product != null) 
        {
            var userIp = GirenIp;

            // Get existing basket entry if any based on session/user id and product id
            Basket basket = dn.basket.FirstOrDefault(x=>x.UserId == userIp && x.ProductId == urunid);
            if (basket == null) 
            {
                // basket does not already exist, so add new basket
                basket = new Basket() 
                {
                    // You can use property assignment with constructors
                    ProductName = product.ProductName,
                    ProductId = product.ProductId,
                    ProductPrice = product.ProductPrice,
                    ProductPic = product.ProductPicture,
                    UserId = userIp,
                    ProductQuantity = 1      // Your initial quantity
                }
                db.Basket.Add(basket);
            } 
            else 
            {
                // Existing basket, just increase the quantity
                basket.ProductQuantity++;
            }
            db.SaveChanges();
        }
    }
}