public ActionResult Create(FormCollection fc, QC_DoiTacTieuBieu obj) { try { if (ModelState.IsValid) { var o = ctx.QC_DoiTacTieuBieu.Where(x => x.ten.ToLower() == obj.ten.ToLower()).FirstOrDefault(); if (o != null) { ModelState.AddModelError("", "Đã tồn tại đối tác này !"); return(View()); } ctx.QC_DoiTacTieuBieu.Add(obj); ctx.SaveChanges(); if (obj.id > 0) { SetAlert("Thêm mới thành công", AlertType.Success); return(RedirectToAction("Index", "QC_DoiTacTieuBieu")); } 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, QC_DoiTacTieuBieu obj) { try { if (ModelState.IsValid) { var check = ctx.QC_DoiTacTieuBieu.Where(x => x.id == obj.id && x.ten.ToLower() == obj.ten.ToLower()).FirstOrDefault(); if (check != null) { ModelState.AddModelError("", "Đã tồn tại đối tác này !"); return(View()); } ctx.Entry(obj).State = EntityState.Modified; int cn = ctx.SaveChanges(); if (cn > 0) { SetAlert("Cập nhật thành công", AlertType.Success); return(RedirectToAction("Index", "QC_DoiTacTieuBieu")); } 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()); } }