public ActionResult PostDetail(int id) { try { using (var db = new DBContext()) { var post = (from p in db.Posts where p.Id == id select p).FirstOrDefault(); db.Entry(post) .Reference(x => x.Poster) .Load(); db.Entry(post) .Reference(x => x.Ward) .Load(); db.Entry(post.Ward) .Reference(x => x.District) .Load(); db.Entry(post.Ward.District) .Reference(x => x.City) .Load(); db.Entry(post) .Collection(x => x.Images) .Load(); post.Comments = (from c in db.Comments where c.PostId == post.Id && c.Approved orderby c.CreateTime select c).ToList(); foreach (var item in post.Comments) { db.Entry(item) .Reference(x => x.User) .Load(); } post.Views = post.Views + 1; ViewPostModel viewPost = new ViewPostModel(); viewPost.UserId = 0; if (Session["userid"] != null) { viewPost.UserId = int.Parse(Session["userid"].ToString()); } viewPost.PostId = post.Id; viewPost.Time = DateTime.Now; db.ViewPosts.Add(viewPost); db.SaveChanges(); return(View(post)); } } catch { return(Json("Đã xảy ra lỗi trong quá trình lấy dữ liệu bài viết", JsonRequestBehavior.AllowGet)); } }
public ActionResult AddComment(ViewPostModel vpm) { if (User.Identity.IsAuthenticated) { vpm.Comment.Email = "RegisteredUser@" + User.Identity.Name + ".com"; vpm.Comment.User = User.Identity.Name; } vpm.Comment.Date = DateTime.Now; if (ModelState.IsValid) { repository.AddComment(vpm.Comment); repository.SaveChanges(); } return(RedirectToAction("FullPost", new { vpm.Comment.PostId })); }
public ActionResult Post(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Article article = db.Articles.Find(id); if (article == null) { return(HttpNotFound()); } var LastestArticle = getLastestArticle(); var check = true; foreach (var item in LastestArticle) { if (item.Id == article.Id) { check = false; break; } } if (check == true) { LastestArticle.Insert(0, article); setLastestArticle(LastestArticle); } var listArticle = db.Articles.Where(p => (int)p.Status == 2 && p.Img != null && p.Img != " ").OrderByDescending(a => a.Count).Take(12); var comment = db.Comments.Where(c => c.ArticleID == article.Id); var viewModel = new ViewPostModel() { Article = article, Comments = comment.ToList(), ListArticle = listArticle.ToList(), LastestArticle = LastestArticle }; article.Count++; db.Entry(article).State = EntityState.Modified; db.SaveChanges(); return(View("~/Views/Client/Post.cshtml", viewModel)); }
public ActionResult FullPost(int postId) { ViewPostModel vp = new ViewPostModel(); vp.Post = repository.FindPostById(postId); if (vp.Post == null) { return(HttpNotFound()); } ViewBag.PostUser = false; if (User.Identity.Name == vp.Post.PostUser) { ViewBag.PostUser = true; } return(View(vp)); }
// // GET: /Post/Details/5 public ActionResult Details(int id = 0) { ViewPostModel post = new ViewPostModel(PostDAO.GetPost(id)); return(View(post)); }