예제 #1
0
        public ActionResult Create(Event rec)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["file"];
                if (file != null && file.FileName != "")
                {
                    string ext = Path.GetExtension(file.FileName);
                    string filename = Guid.NewGuid().ToString() + ext;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename);
                    file.SaveAs(filePath);
                    rec.FileUrl = "/Uploads/" + filename;
                }
                rec.CreateAt = string.Format("{0:yyyy-MMMM-dd}", DateTime.Now);
                db.Event.Add(rec);
                db.SaveChanges();
                return RedirectToAction("Event", "Admin");
            }

            return View(rec);
        }
예제 #2
0
        public ActionResult Edit(Event rec)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["file"];
                DbEntities tempdb = new DbEntities();
                Event ori = tempdb.Event.Find(rec.Id);
                if (file != null && file.FileName != "")
                {
                    if (ori.FileUrl != null)
                    {
                        string oriFilePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), Path.GetFileName(ori.FileUrl));
                        if (System.IO.File.Exists(oriFilePath))
                        {
                            System.IO.File.Delete(oriFilePath);
                        }
                    }
                    string ext = Path.GetExtension(file.FileName);
                    string filename = Guid.NewGuid().ToString() + ext;
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), filename);
                    file.SaveAs(filePath);
                    rec.FileUrl = "/Uploads/" + filename;
                }
                else
                {
                    rec.FileUrl = ori.FileUrl;
                }

                db.Entry(rec).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Event", "Admin");
            }
            return View(rec);
        }