public void PrepareBlogpostModel(AllBlogsModel model, List <BlogPost.Core.BlogPost> post) { foreach (var item in post) { BlogPostModel posts = new BlogPostModel() { Description = item.Description, IsPrivate = item.IsPrivate, Name = item.Name, UserName = _context.User.Where(a => a.Id == item.UserId).FirstOrDefault() == null ? "" : _context.User.Where(a => a.Id == item.UserId).FirstOrDefault().Name }; model.AllBLogs.Add(posts); } }
public ActionResult AllBlogs() { AllBlogsModel model = new AllBlogsModel(); var UserId = 0; if (Session["UserId"] != null) { UserId = Convert.ToInt32(Session["UserId"]); } var Allblog = _context.Blog.Where(a => a.IsPrivate == false).ToList(); PrepareBlogpostModel(model, Allblog); if (UserId > 0) { var CurrentUserBlog = _context.Blog.Where(c => c.IsPrivate == true && c.UserId == UserId).ToList(); PrepareBlogpostModel(model, CurrentUserBlog); } return(View(model)); }