public ActionResult NewsInfo(NEWS news, HttpPostedFileBase file) { if (Session["login"] is null) { return(RedirectToAction("Login")); } int idRole = ((USER)Session["login"]).IdRole; if (idRole != 1 && idRole != 2) { return(RedirectToAction("Index")); } if (Session["login"] is null) { return(RedirectToAction("Login")); } try { if (String.IsNullOrEmpty(news.NewsTitle)) { throw new Exception("Tên bài viết không được bỏ trống"); } using (THUONGMAIDIENTUEntities db = new THUONGMAIDIENTUEntities()) { if (file != null) { if (!file.ContentType.Contains("image")) { throw new Exception("File hình không hợp lệ"); } if (file.ContentLength > 5 * 1024 * 1024) { throw new Exception("Hình ảnh vượt quá 5Mb"); } string mapPath = Server.MapPath("~/img/tintuc"); string filename = news.NewsTitle.ToLower().Trim().Replace(" ", " ").Replace(" ", "-"); filename = RemoveVietnamese.RemoveSign4VietnameseString(filename).Replace("[", String.Empty).Replace("]", String.Empty).Replace("(", String.Empty).Replace(")", String.Empty).Replace(",", String.Empty).Replace(".", String.Empty).Replace("@", String.Empty).Replace("'", String.Empty).Replace("\"", String.Empty).Replace("\\", String.Empty).Replace("/", String.Empty) + ".png"; string filePath = mapPath + "/" + filename; try { if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } } catch { } file.SaveAs(filePath); news.NewsThumbail = filename; } if (news.IdNews == 0) { news.IdUser = ((USER)Session["login"]).IdUser; news.DateCreate = DateTime.Now; db.NEWS.Add(news); } else { var newsDB = db.NEWS.Where(x => x.IdNews == news.IdNews).FirstOrDefault(); if (newsDB is null) { throw new Exception("Không tìm thấy bài viết này, vui lòng thử lại"); } newsDB.IdCategory = news.IdCategory; newsDB.NewsDetail = news.NewsDetail; newsDB.NewsThumbail = news.NewsThumbail; newsDB.NewsSumary = news.NewsSumary; newsDB.NewsTitle = news.NewsTitle; news = newsDB; } db.SaveChanges(); } ViewBag.Success = "Bài viết được lưu thành công"; ViewBag.Id = news.IdNews; } catch (Exception ex) { ViewBag.Error = ex.Message; } return(View(news)); }
public ActionResult ProductEdit(PRODUCT product, HttpPostedFileBase[] files) { if (Session["login"] is null) { return(RedirectToAction("Login")); } int idRole = ((USER)Session["login"]).IdRole; if (idRole != 1 && idRole != 2) { return(RedirectToAction("Index")); } try { if (String.IsNullOrEmpty(product.ProductName)) { throw new Exception("Tên sản phẩm không được bỏ trống"); } using (THUONGMAIDIENTUEntities db = new THUONGMAIDIENTUEntities()) { bool imgChanged = false; if (files != null) { int i = 1; foreach (var file in files) { if (file is null) { continue; } imgChanged = true; if (!file.ContentType.Contains("image")) { throw new Exception("File hình không hợp lệ"); } if (file.ContentLength > 5 * 1024 * 1024) { throw new Exception("Hình ảnh vượt quá 5Mb"); } string mapPath = Server.MapPath("~/img/product"); string filename = product.ProductName.ToLower().Trim().Replace(" ", " ").Replace(" ", "-"); filename = RemoveVietnamese.RemoveSign4VietnameseString(filename).Replace("[", String.Empty).Replace("]", String.Empty).Replace("(", String.Empty).Replace(")", String.Empty).Replace(",", String.Empty).Replace(".", String.Empty).Replace("@", String.Empty).Replace("'", String.Empty).Replace("\"", String.Empty).Replace("\\", String.Empty).Replace("/", String.Empty) + "-" + i + ".png"; string filePath = mapPath + "/" + filename; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } file.SaveAs(filePath); PRODUCT_IMG img = new PRODUCT_IMG(); img.Filename = filename; img.ImgAlt = RemoveVietnamese.RemoveSign4VietnameseString(product.ProductName); product.PRODUCT_IMG.Add(img); i++; } } if (product.IdProduct == 0) { product.IdUser = ((USER)Session["login"]).IdUser; db.PRODUCTs.Add(product); } else { var productDB = db.PRODUCTs.Where(x => x.IdProduct == product.IdProduct).FirstOrDefault(); if (productDB is null) { throw new Exception("Không tìm thấy sản phẩm này, vui lòng thử lại"); } productDB.IdCategory = product.IdCategory; productDB.ProductName = product.ProductName; productDB.ProductPrice = product.ProductPrice; productDB.ProductSumary = product.ProductSumary; productDB.ProductDetail = product.ProductDetail; if (imgChanged) { if (product.PRODUCT_IMG != null) { foreach (var item in productDB.PRODUCT_IMG.ToList()) { db.PRODUCT_IMG.Remove(item); } } foreach (var item in product.PRODUCT_IMG.ToList()) { productDB.PRODUCT_IMG.Add(item); } } productDB.PRODUCT_INFO.Clear(); if (product.PRODUCT_INFO != null) { foreach (var item in product.PRODUCT_INFO.ToList()) { productDB.PRODUCT_INFO.Add(item); } } product = productDB; } db.SaveChanges(); } ViewBag.Success = "Bài viết được lưu thành công"; ViewBag.Id = product.IdProduct; } catch (Exception ex) { ViewBag.Error = ex.Message; } return(View(product)); }