如何控制数量和增加
本文关键字:增加 控制 何控制 | 更新日期: 2023-09-27 18:06:29
我使用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 = sessionID来自ajax, urunId = ProductId来自ajax, 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; }
}
}
您不希望在同一个对象上运行多个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();
}
}
}