public ActionResult Create(Writing writing) { if (ModelState.IsValid) { HttpPostedFileBase f = Request.Files["Url"]; if (f != null) { string url = "~/Upload/Writing/" + "day_" + DateTime.Now.ToString("yyMMdd") + "/"; string extension = Code.Tool.GetFileExtension(f.FileName); string upext = "jpg,jpeg,gif,png"; if (("," + upext + ",").IndexOf("," + extension + ",") < 0) { return View(writing); } string filePath = HttpContext.Server.MapPath(url); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } Random random = new Random(DateTime.Now.Millisecond); string fname = DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(10000) + "." + extension; filePath += fname; f.SaveAs(filePath); writing.Url = url + fname; } writing.Posted = DateTime.Now; writing.Like = 0; db.Writings.Add(writing); db.SaveChanges(); return RedirectToAction("Index"); } return View(writing); }
public ActionResult Edit(Writing writing) { if (ModelState.IsValid) { db.Entry(writing).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(writing); }