public ActionResult AddForum(Plate model, HttpPostedFileBase file) { if (ModelState.IsValid) { try { if (file != null) { System.IO.Stream stream = file.InputStream; byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); stream.Close(); model.Picture = buffer; } model.Time = DateTime.Now; model.Report = 0; model.IsColse = false; model.Browses = 0; db.Plates.Add(model); db.SaveChanges(); return Redirect("/Admin/Bbs/Forum"); } catch { ModelState.AddModelError("", "增加板块失败"); } } else { ModelState.AddModelError("", "信息填写错误!"); } return View(); }
public ActionResult Add(int id = 0) { Plate plate = new Plate(); List<Plate> platelist = new List<Plate>(); platelist = (from pl in db.Plates where pl.IsColse == false select pl).ToList(); ViewBag.platelist = platelist; if (id == 0) { ViewBag.ptitle = null; } else { plate = (from p in db.Plates where p.ID == id select p).FirstOrDefault(); ViewBag.ptitle = plate.Title; } ViewBag.plateID = id; return View(); }
public ActionResult ForumEdit(Plate model, HttpPostedFileBase file) { var _forum = db.Plates.Find(model.ID); if (ModelState.IsValid) { try { var forum = db.Plates.Find(model.ID); if (file != null) { System.IO.Stream stream = file.InputStream; byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); stream.Close(); forum.Picture = buffer; } forum.Title = model.Title; forum.Description = model.Description; forum.IsColse = model.IsColse; var topics = db.Topics.Where(x => x.PlateID == model.ID).ToList(); foreach (Topic item in topics) { item.IsColse = model.IsColse; } db.SaveChanges(); return Redirect("/Admin/Bbs/Forum"); } catch { ModelState.AddModelError("", "修改板块失败!"); } } else { ModelState.AddModelError("", "填写信息不正确!"); } return View(_forum); }