public async Task <ActionResult> AddPost(Posts model) { using (var ctx = new BlogHealthEntities()) { model.CreateDate = DateTime.Now; ctx.Posts.Add(model); try { ctx.SaveChanges(); TempData["Success"] = "Đăng thành công!"; return(RedirectToAction("AddPost", "Admin")); } catch (Exception ex) { var post = ctx.Posts.Where(c => c.ID == model.ID).FirstOrDefault(); if (post != null) { ctx.Posts.Remove(post); ctx.SaveChanges(); } TempData["Error"] = "Lỗi đăng bài. " + ex.Message; return(RedirectToAction("AddPost", "Admin")); } } }
public ActionResult DeleteCategories(int?ID) { if (!ID.HasValue) { return(Json(new { result = 0, error = "ID không tồn tại" }, JsonRequestBehavior.AllowGet)); } using (var ctx = new BlogHealthEntities()) { var cate = ctx.Categories.Where(c => c.ID == ID).FirstOrDefault(); try { if (cate != null) { ctx.Categories.Remove(cate); ctx.SaveChanges(); return(Json(new { result = 1, error = "" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { result = 0, error = "Lỗi" }, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { return(Json(new { result = 0, error = ex.Message }, JsonRequestBehavior.AllowGet)); } } }
public ActionResult UpdatePost(Posts model) { using (var ctx = new BlogHealthEntities()) { var post = ctx.Posts.Where(c => c.ID == model.ID).FirstOrDefault(); if (post == null) { return(Json(new { result = "0", msg = "Bài viết không tồn tại!" }, JsonRequestBehavior.AllowGet)); } try { post.Title = model.Title; post.Slug = model.Slug; post.IDCategory = model.IDCategory; post.ShortContent = model.ShortContent; post.Content = model.Content; post.Tag = model.Tag; post.Update = DateTime.Now; ctx.Entry(post).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); return(Json(new { result = "1", msg = "Cập nhật thành công!" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { result = "0", msg = ex.Message }, JsonRequestBehavior.AllowGet)); } } }
public ActionResult AddCategory(Categories model) { if (String.IsNullOrEmpty(model.Name)) { return(Json("0", JsonRequestBehavior.AllowGet)); } using (var ctx = new BlogHealthEntities()) { ctx.Categories.Add(model); ctx.SaveChanges(); } return(Json("1", JsonRequestBehavior.AllowGet)); }
public ActionResult UpdateCategories(Categories model) { using (var ctx = new BlogHealthEntities()) { var cate = ctx.Categories.Where(c => c.ID == model.ID).FirstOrDefault(); if (cate != null) { cate.Name = model.Name; cate.Priority = model.Priority; cate.Slug = model.Slug; cate.Color = model.Color; ctx.Entry(cate).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); return(Json("1", JsonRequestBehavior.AllowGet)); } else { return(Json("0", JsonRequestBehavior.AllowGet)); } } }
public ActionResult DeletePost(int?ID) { if (!ID.HasValue) { return(Json(new { result = 0, msg = "Không tồn tại mã!" }, JsonRequestBehavior.AllowGet)); } using (var ctx = new BlogHealthEntities()) { try { var post = ctx.Posts.Where(c => c.ID == ID.Value).FirstOrDefault(); ctx.Posts.Remove(post); ctx.SaveChanges(); return(Json(new { result = 1, msg = "Thành công" }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(Json(new { result = 0, msg = ex.Message }, JsonRequestBehavior.AllowGet)); } } }