public ActionResult EditImg(string urlImgList) { try { var img_ = urlImgList.Split(';'); var loaihinhanhId = Convert.ToInt32(Session["idLoaiHinhAnh"]); var listdel = db.admin_hinhanh.Where(s => s.loaihinhanh == loaihinhanhId); foreach (var item1 in listdel) { using (var db1 = new WebApplication1Entities1()) { try { var objhinhanh = db1.admin_hinhanh.Find(item1.id); db1.admin_hinhanh.Attach(objhinhanh); db1.admin_hinhanh.Remove(objhinhanh); db1.SaveChanges(); } catch (Exception ex) { throw; } } } using (var db2 = new WebApplication1Entities1()) { foreach (var item in img_) { if (item.Length > 0) { admin_hinhanh admin_hinhanh = new admin_hinhanh(); admin_hinhanh.tenhinhanh = "img" + DateTime.Now.Year.ToString() + "" + DateTime.Now.Month + "" + DateTime.Now.Day; admin_hinhanh.urlhinhanh = item; admin_hinhanh.ngaytao = DateTime.Now; admin_hinhanh.ngaycapnhat = DateTime.Now; admin_account u = Session["User"] as admin_account; admin_hinhanh.nguoitao = u.id; admin_hinhanh.loaihinhanh = loaihinhanhId; db2.admin_hinhanh.Add(admin_hinhanh); db2.SaveChanges(); } } } return(RedirectToAction("Edit", "LoaiHinhAnh", new { id = loaihinhanhId })); // Redirect to your NextView } catch (Exception ex) { throw; } }