public async Task <ActionResult> InsertNews() { return(await Task.Run(() => { ViewBag.Message = "Insert new news."; var stateNews = new CreateStateNews(); stateNews.States.AddRange(StateCodeBL.Instance.SelectStates()); return View(stateNews); })); }
public async Task <ActionResult> InsertNews(CreateStateNews newForInsert) { return(await Task.Run(() => { var dbHelprer = DBHelper.Instance; var BLInstance = StateNewsBL.Instance; dbHelprer.BeginTransaction(); try { if (ModelState.IsValid) { var news = new StateNews { Heading = newForInsert.Heading.Trim(), ShortDescription = newForInsert.ShortDesc.Trim(), NewsDescription = string.IsNullOrEmpty(newForInsert.Description) ? string.Empty : newForInsert.Description.Trim(), StateCode = newForInsert.StateCode, IsTopNews = newForInsert.IsTopTenNews ? 1 : 0, IsActive = 0, IsApproved = 0 }; var image = new ImageDetail { ImageType = (int)ImageType.TopNewsImage, ImageUrl = " ", Caption = newForInsert.Caption, CaptionLink = newForInsert.CaptionLink, IsActive = 0, IsFirst = 0 }; var fileName = string.Empty; if (newForInsert.Image != null && newForInsert.Image.ContentLength > 0) { fileName = new Random().Next(1000000000).ToString() + Path.GetFileName(newForInsert.Image.FileName); image.ImageUrl = ImagePath.TopTenNewsImage + "/" + fileName; image.IsActive = 1; image.IsFirst = 1; } string dirPath = ImagePath.StateNewsImage; var user = (IUsers)this.Session[Constants.UserSessionKey]; BLInstance.Insert(ref dbHelprer, news, image, user); if (SaveImage(ImagePath.StateNewsImage, fileName, newForInsert.Image)) { newForInsert = new CreateStateNews(); newForInsert.States.AddRange(StateCodeBL.Instance.SelectStates()); ViewBag.Message = "Successfully Inserter."; ModelState.Clear(); } } else { ModelState.AddModelError("INSERT", "Oops ! There is some error."); ViewBag.Message = "Oops ! There is some error."; } dbHelprer.CommitTransaction(); return View(newForInsert); } catch (Exception ex) { dbHelprer.RollbackTransaction(); throw ex; } finally { dbHelprer.Dispose(); BLInstance.Dispose(); } })); }