public ActionResult Create(FormCollection fc, JF_SanPham obj) { try { TempData.Keep("loaisanpham"); TempData.Keep("donvitinh"); if (ModelState.IsValid) { obj.hientrangchu = obj.hientrangchu == null ? false : true; obj.noibat = obj.noibat == null ? false : true; obj.kichhoat = true; obj.ngaycapnhat = DateTime.Now; ctx.JF_SanPham.Add(obj); ctx.SaveChanges(); if (obj.id > 0) { SetAlert("Thêm mới sản phẩm thành công", AlertType.Success); return(RedirectToAction("Index", "JF_SanPham")); } else { ModelState.AddModelError("", "Thêm mới không thành công"); } return(View("Index")); } return(View()); } catch (Exception ex) { SetAlert("Lỗi" + ex.Message.ToString(), AlertType.Error); Logs.WriteLog(ex); return(View()); } }
public ActionResult Edit(FormCollection fc, JF_SanPham obj) { try { TempData.Keep("loaisanpham"); TempData.Keep("donvitinh"); if (ModelState.IsValid) { var o = ctx.JF_SanPham.Find(obj.id); o.ten = obj.ten; o.giatruockm = obj.giatruockm; o.giatien = obj.giatien; o.id_donvitinh = obj.id_donvitinh; o.id_loaisanpham = obj.id_loaisanpham; o.hinhdaidien = obj.hinhdaidien; o.hientrangchu = obj.hientrangchu == null ? false : true; o.noibat = obj.noibat == null ? false : true; o.mota = obj.mota; ctx.Entry(o).State = EntityState.Modified; int cn = ctx.SaveChanges(); if (cn > 0) { SetAlert("Cập nhật thành công", AlertType.Success); return(RedirectToAction("Index", "JF_SanPham")); } else { SetAlert("Cập nhật không thành công", AlertType.Error); } } return(View(obj)); } catch (Exception ex) { SetAlert("Lỗi" + ex.Message.ToString(), AlertType.Error); Logs.WriteLog(ex); return(View()); } }