public ActionResult OnEdit(BannerModels banner) { if (ModelState.IsValid) { banner.ImageUrl = banner.Image != null? banner.Image.Upload() : banner.ImageUrl.ToImageOriginalPath(); var bannerUpdate = banner.CloneTo <Banner>(new[] { nameof(banner.Image), nameof(banner.StartDate), nameof(banner.EndDate) }); bannerUpdate.StartDate = banner.StartDate; bannerUpdate.EndDate = banner.EndDate; var result = BannerService.Update(bannerUpdate); if (result == Result.NotExists) { ModelState.AddModelError("", "Banner không tồn tại trên hệ thống."); ViewBag.ListPosition = DataHelper.ListEnumType <PositionEnum>(); return(View("Edit", banner)); } SetFlashMessage($"Cập nhật Banner '{banner.Name}' thành công."); if (banner.SaveList) { return(RedirectToAction("Index")); } ViewBag.ListPosition = DataHelper.ListEnumType <PositionEnum>(); return(View("Edit", banner)); } ViewBag.ListPosition = DataHelper.ListEnumType <PositionEnum>(); return(View("Edit", banner)); }
public ActionResult OnCreate(BannerModels banner) { if (ModelState.IsValid) { banner.ImageUrl = banner.Image != null? banner.Image.Upload() : banner.ImageUrl; var newBanner = banner.CloneTo <Banner>(new[] { nameof(banner.Image), nameof(banner.StartDate), nameof(banner.EndDate) }); newBanner.StartDate = banner.StartDate; newBanner.EndDate = banner.EndDate; var result = BannerService.Insert(newBanner); if (result == Result.Exists) { ModelState.AddModelError("", $"Banner '{banner.Name}' đã tồn tại trên hệ thống."); ViewBag.ListPosition = DataHelper.ListEnumType <PositionEnum>(); return(View("Create", banner)); } SetFlashMessage($"Thêm banner '{banner.Name}' thành công."); if (banner.SaveList) { return(RedirectToAction("Index")); } ModelState.Clear(); ViewBag.ListPosition = DataHelper.ListEnumType <PositionEnum>(); return(View("Create", banner.ResetValue())); } ViewBag.ListPosition = DataHelper.ListEnumType <PositionEnum>(); return(View("Create", banner)); }