public ActionResult Index(string searchString, int?page) { if (Session["MaTKAdmin"] == null) { return(RedirectToAction("Login", "Admin")); } else { try { QuanLyTinTuc model = new QuanLyTinTuc() { DanhSachTinTuc = KhoiTao(searchString, page) }; return(View(model)); } catch (Exception) { return(RedirectToAction("Index")); } } }
public ActionResult ThemTinTuc(QuanLyTinTuc qltt, HttpPostedFileBase file, string submit, int id, string hdimg) { if (Session["MaTKAdmin"] == null) { return(RedirectToAction("Login", "Admin")); } else { try { ShopEntities shop = new ShopEntities(); if (submit == "Thêm") { file = file ?? Request.Files["file"]; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); if (fileName != null) { var path = Path.Combine(Server.MapPath("~/Images/TinTuc/"), fileName); file.SaveAs(path); } TinTuc tt = new TinTuc(); tt.TieuDe = qltt.TinTuc.TieuDe; tt.AnhDaiDien = "/Images/TinTuc/" + fileName; tt.MoTa = qltt.TinTuc.MoTa; tt.NgayTao = DateTime.Now; tt.ChiTiet = qltt.TinTuc.ChiTiet; tt.LuotXem = 0; shop.TinTucs.Add(tt); shop.SaveChanges(); } } if (submit == "Sửa") { file = file ?? Request.Files["file"]; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); if (fileName != null) { var path = Path.Combine(Server.MapPath("~/Images/TinTuc/"), fileName); file.SaveAs(path); var tt = shop.TinTucs.Single(s => s.MaTinTuc == id); tt.TieuDe = qltt.TinTuc.TieuDe; tt.AnhDaiDien = "/Images/TinTuc/" + fileName; tt.MoTa = qltt.TinTuc.MoTa; tt.ChiTiet = qltt.TinTuc.ChiTiet; shop.SaveChanges(); } } else { var tt = shop.TinTucs.Single(s => s.MaTinTuc == id); tt.TieuDe = qltt.TinTuc.TieuDe; tt.AnhDaiDien = hdimg; tt.MoTa = qltt.TinTuc.MoTa; tt.ChiTiet = qltt.TinTuc.ChiTiet; shop.SaveChanges(); } } // KhoiTao(); QuanLyTinTuc model = new QuanLyTinTuc() { DanhSachTinTuc = KhoiTao("", 1) }; return(View("Index", model)); } catch (Exception) { return(RedirectToAction("Index")); } } }