Esempio n. 1
0
 public ActionResult Create(FormCollection fc, JF_DonViTinh obj)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var dvt = ctx.JF_DonViTinh.Where(x => x.ten == obj.ten && x.kichhoat == true).FirstOrDefault();
             if (dvt != null)
             {
                 ModelState.AddModelError("", "Đã tồn tại đơn vị tính này !");
                 return(View());
             }
             obj.kichhoat = true;
             ctx.JF_DonViTinh.Add(obj);
             ctx.SaveChanges();
             if (obj.id > 0)
             {
                 SetAlert("Thêm mới loại sản phẩm thành công", AlertType.Success);
                 return(RedirectToAction("Index", "JF_DonViTinh"));
             }
             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());
     }
 }
Esempio n. 2
0
 public ActionResult Edit(FormCollection fc, JF_DonViTinh obj)
 {
     try
     {
         if (ModelState.IsValid)
         {
             var o = ctx.JF_DonViTinh.Where(x => x.ten.ToLower() == obj.ten.ToLower() && x.kichhoat == true).FirstOrDefault();
             if (o.id != obj.id)
             {
                 ModelState.AddModelError("", "Đã tồn tại dvt này !");
                 return(View());
             }
             o.ten              = obj.ten;
             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_DonViTinh"));
             }
             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());
     }
 }