我需要帮助这个标题 控制器
public void SaveOrder(int magiam, string name, string phone)
{
ViewCart cartItm = new ViewCart();
DonHang dh = new DonHang();
//od.tblOrderDetails = details;
dh.NgayTaoDH = DateTime.Now;
dh.TenTK= Session["NhanVien"].ToString();
Random cus_id = new Random();
dh.MaKH = cus_id.Next();
db.DonHangs.Add(dh);
KhachHang kh = new KhachHang();
db.KhachHangs.Add(kh);
kh.MaKH = (int)dh.MaKH;
kh.TenKH = name;
kh.SDTKH = phone;
db.KhachHangs.Add(kh);
List<ViewCart> viewcart = (List<ViewCart>)Session["Cart"];
foreach (var itm in viewcart)
{
ChiTietDonHang ctdh = new ChiTietDonHang();
ctdh.MaDH = dh.MaDH;
ctdh.IDSP = itm.ID;
ctdh.SoLuong = itm.SoLuong;
ctdh.Gia = itm.Gia;
ctdh.GiamGia = magiam;
dh.TongGia = viewcart.Sum(p=>p.Gia*p.SoLuong) - (viewcart.Sum(p => p.Gia * p.SoLuong)*magiam)/100;
db.ChiTietDonHangs.Add(ctdh);
var model1 = db.SanPhams.ToList();
foreach (var item in model1)
{
item.SoLuongSP = item.SoLuongSP - itm.SoLuong;
db.SanPhams.Add(item);
}
}
db.SaveChanges();
}
当我将商品添加到购物车并点击付款完成时,我有错误
System.Data.Entity.Validation.DbEntityValidationException:&#39;验证 一个或多个实体失败。请参阅&#39; EntityValidationErrors&#39;属性 了解更多详情。&#39;
无法保存。
var model1 = db.SanPhams.ToList();
foreach (var item in model1)
{
item.SoLuongSP = item.SoLuongSP - itm.SoLuong;
db.SanPhams.Add(item);
}
此代码关于减少数量
有人检查并帮助我,如果新的方式请帮助我。 谢谢