public ActionResult Create(UCNhapKhoModel model) { if (model.DSChitiet.Count() == 0) { return(BadRequest()); } UcnhapKho nhapkho = new UcnhapKho(); nhapkho = model.CopyAs <UcnhapKho>(); nhapkho.Id = Guid.NewGuid(); nhapkho.NgayTao = DateTime.Now; nhapkho.IdTrangThai = _context.CmtuDien.Where(x => x.MaTuDien == model.TrangThai).FirstOrDefault().Id; _context.UcnhapKho.Add(nhapkho); foreach (var item in model.DSChitiet) { UcchiTietNhapKho ct = new UcchiTietNhapKho(); ct = item.CopyAs <UcchiTietNhapKho>(); ct.Id = Guid.NewGuid(); ct.IdNhapKho = nhapkho.Id; _context.UcchiTietNhapKho.Add(ct); } _context.SaveChanges(); return(NoContent()); }
public ActionResult Update(UCNhapKhoModel model) { if (model.DSChitiet.Count() == 0) { return(BadRequest()); } var data = _context.UcnhapKho.Find(model.Id); data.Ten = model.Ten; data.SoHd = model.SoHd; data.GhiChu = model.GhiChu; data.TongTien = model.TongTien; data.IdTrangThai = _context.CmtuDien.Where(x => x.MaTuDien == model.TrangThai).FirstOrDefault().Id; _context.UcnhapKho.Add(data); foreach (var item in model.DSChitiet) { var ct = _context.UcchiTietNhapKho.Find(item.Id); ct.Gia = item.Gia; ct.SoLuong = item.SoLuong; _context.UcchiTietNhapKho.Add(ct); } _context.SaveChanges(); return(NoContent()); }