public IActionResult Create(CreateBanner model) { if (ModelState.IsValid) { if (bannerRepositori.CreateBanner(model) > 0) { return(RedirectToAction(actionName: "Index", controllerName: "Home")); } else { ModelState.AddModelError("", "Tên Danh Mục Đả Tồn Tại"); return(View(model)); } } return(View(model)); }
public IActionResult Create(CreateBanner model) { ViewBag.Categories = bannerRepositori.Gets(); if (ModelState.IsValid) { if (bannerRepositori.CreateBanner(model) > 0) { return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Tên này đã tồn tại, vui lòng thử lại tên khác!"); } } return(View(model)); }
public int CreateBanner(CreateBanner createBanner) { var count = 0; foreach (var item in context.banners) { if (item.BannerName == createBanner.BannerName) { count++; } } if (count == 0) { var fileName = string.Empty; if (createBanner.BannerImage != null) { string uploadFolder = Path.Combine(webHostEnvironment.WebRootPath, "images/Banner"); fileName = $"{Guid.NewGuid()}_{createBanner.BannerImage.FileName}"; var filePath = Path.Combine(uploadFolder, fileName); using (var fs = new FileStream(filePath, FileMode.Create)) { createBanner.BannerImage.CopyTo(fs); } } if (createBanner.BannerImage == null) { fileName = "nonCat.jpg"; } Banner newBanner = new Banner() { BannerName = createBanner.BannerName, BannerPhoto = fileName, Description = createBanner.Description }; context.banners.Add(newBanner); return(context.SaveChanges()); } return(0); }