//[ValidateAntiForgeryToken] public ActionResult CreateNew(New news, HttpPostedFileBase ImageFile, ImageNew imageNew) { if (ImageFile != null) { string fileExtention = Path.GetExtension(ImageFile.FileName); string fileName = Guid.NewGuid().ToString() + fileExtention.ToString(); ImageFile.SaveAs(Server.MapPath("~/NewsImage/" + fileName)); imageNew.ImageName = fileName; imageNew.NewId = news.Id; db.News.Add(news); db.ImageNews.Add(imageNew); db.SaveChanges(); ModelState.Clear(); return(RedirectToAction("Blog")); } return(RedirectToAction("Blog")); }
public ActionResult Form(Report report, HttpPostedFileBase file) { using (MyDbContext ctx = new MyDbContext()) { if (report == null) { return(RedirectToAction("Index")); } string userId = User.Identity.GetUserId(); report.UserId = userId; var categories = ctx.Categories.ToList(); ViewBag.Categories = categories; if (report.Id == 0) { if (report.CategoryId == 0) { report.CategoryId = null; } report.CreatedDate = DateTime.Now; report.UpdatedDate = DateTime.Now; report.Status = (byte)Status.hold; ctx.Entry(report).State = System.Data.Entity.EntityState.Added; ctx.SaveChanges(); string ImgPath = ""; if (file != null) { ImgPath = Addfile(file, "~/Files/banners"); ImageNew imageNew = new ImageNew(); imageNew.NewsId = report.Id; imageNew.Path = ImgPath; report.Status = (byte)Status.hold; ctx.Entry(imageNew).State = System.Data.Entity.EntityState.Added; report.ImageId = imageNew.Id; } ctx.SaveChanges(); } else { if (report.CategoryId == 0) { report.CategoryId = null; } report.UpdatedDate = DateTime.Now; ctx.Entry(report).State = System.Data.Entity.EntityState.Modified; string ImgPath = ""; if (file != null) { ImgPath = Addfile(file, "~/Files/banners"); ImageNew imageNew = new ImageNew(); imageNew.NewsId = report.Id; imageNew.Path = ImgPath; ctx.Entry(imageNew).State = System.Data.Entity.EntityState.Added; report.ImageId = imageNew.Id; } ctx.SaveChanges(); } ctx.SaveChanges(); return(RedirectToAction("Index")); } }