コード例 #1
0
        public ActionResult Edit([Bind(Include = "Id,Title,Description,SubDescription,CoverImage,DateTime,CategoryId,RegionId")] News news, HttpPostedFileBase cover)
        {
            if (ModelState.IsValid)
            {
                if (cover != null && cover.ContentLength > 0)
                {
                    string hash = MD5Helper.Calculate(cover);

                    string filename = Path.Combine(HttpContext.Server.MapPath("~/images"), hash + "_" + cover.FileName);

                    cover.SaveAs(filename);

                    news.CoverImage = Path.Combine("\\images", hash + "_" + cover.FileName).Replace("\\", "/");
                }


                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", news.CategoryId);
            ViewBag.RegionId   = new SelectList(db.Regions, "Id", "Name", news.RegionId);
            ViewBag.UserId     = new SelectList(db.Users, "Id", "UserName", news.UserId);
            ViewBag.CoverImage = new SelectList(db.NewsImages, "Id", "Image", news.CoverImage);
            return(View(news));
        }