提问人:Dương Tô 提问时间:11/17/2023 更新时间:11/17/2023 访问量:52
无法将值 NULL 插入到列(字符串格式列)[关闭]
Cannot insert the value NULL into column (string format column) [closed]
问:
SqlException:无法将值 NULL 插入到表“Nhom9BH.dbo.HoaDon”的列“Address”中;列不允许 null。INSERT 失败。该声明已被终止。 我只是看了一些 youtube 视频,它是关于 Id 等列的,我的情况是一个字符串,我无法用这些解决方案修复。有人可以帮我吗 我的行动:
public IActionResult Checkout()
{
var identity = (ClaimsIdentity)User.Identity;
var claim = identity.FindFirst(ClaimTypes.NameIdentifier);
CartViewModel Cart = new CartViewModel()
{
CartList = _db.Cart.Include("Product")
.Where(gh => gh.ApplicationUserId == claim.Value)
.ToList(),
Bill = new Bill()
};
Cart.Bill.ApplicationUser = _db.ApplicationUser.FirstOrDefault(user => user.Id == claim.Value);
Cart.Bill.Name = Cart.Bill.ApplicationUser.Name;
Cart.Bill.Address = Cart.Bill.ApplicationUser.Address;
Cart.Bill.PhoneNumber = Cart.Bill.ApplicationUser.PhoneNumber;
foreach (var item in Cart.CartList)
{
item.ProductPrice = item.Quantity * item.Product.Price;
Cart.Bill.TotalPrice += item.ProductPrice;
}
return View(Cart);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Checkout(CartViewModel Cart)
{
var identity = (ClaimsIdentity)User.Identity;
var claim = identity.FindFirst(ClaimTypes.NameIdentifier);
Cart.CartList = _db.Cart.Include(x => x.Product)
.Where(gh => gh.ApplicationUserId == claim.Value)
.ToList();
if (Cart.Bill == null)
{
Cart.Bill = new Bill();
}
Cart.Bill.ApplicationUserId = claim.Value;
Cart.Bill.OrderDate = DateTime.Now;
Cart.Bill.OrderStatus = "Confirming";
foreach (var item in Cart.CartList)
{
item.ProductPrice = item.Quantity + item.Product.Price;
Cart.Bill.TotalPrice += item.ProductPrice;
}
_db.Bill.Add(Cart.Bill);
_db.SaveChanges();
foreach (var item in Cart.CartList)
{
BillDetail BillDetail = new BillDetail()
{
ProductId = item.ProductId,
BillId = Cart.Bill.Id,
ProductPrice = item.ProductPrice,
Quantity = item.Quantity
};
_db.BillDetail.Add(BillDetail);
_db.SaveChanges();
}
_db.Cart.RemoveRange(Cart.CartList);
_db.SaveChanges();
return RedirectToAction("Index", "Home");
}
我尝试观看印度视频,但找不到任何合适的解决方案
答: 暂无答案
评论
Address
Cart.Bill.Address = "";