public async Task <IActionResult> Index(int page) { var posted = _context.Posts.Where(p => p.IsPublished).ToList().Count; if (page < 0) { page = 0; } if (page * 5 > _context.Posts.ToList().Count) { page = _context.Posts.ToList().Count / 5; } var posts = _context.Posts .Where(p => p.IsPublished) .OrderByDescending(p => p.Created) .Include(p => p.Blogs) .Skip(page * 5).Take(5); var blogs = _context.Blogs; var tags = _context.Tags; BlogPostsVM categories = new BlogPostsVM() { Blogs = await blogs.ToListAsync(), Posts = await posts.ToListAsync(), Tags = await tags.ToListAsync(), PageNumber = page, TotalPosts = _context.Posts.ToList().Count }; return(View(categories)); }
public async Task <IActionResult> Tag() { var name = RouteData.Values["id"].ToString(); var posts = _context.Tags.Where(t => t.Name == name).Select(t => t.Posts); var blogs = _context.Blogs; BlogPostsVM categories = new BlogPostsVM() { Blogs = await blogs.ToListAsync(), Posts = await posts.ToListAsync(), Tags = await _context.Tags.ToListAsync() }; return(View("Index", categories)); }
public async Task <IActionResult> Categories() { var id = RouteData.Values["id"].ToString(); var posts = _context.Posts.Where(p => p.BlogId == Int32.Parse(id) && p.IsPublished == true).Include(p => p.Blogs); var blogs = _context.Blogs; var tags = _context.Tags; BlogPostsVM categories = new BlogPostsVM() { Blogs = await blogs.ToListAsync(), Posts = await posts.ToListAsync(), Tags = await tags.ToListAsync() }; return(View("Index", categories)); }
public IActionResult BlogPosts() { var sportOptionsList = Sport.All.GetOptionsWithAll(); var userEmail = User.GetUserEmail(); var tableItems = _db.Articles.Include(a => a.User).Where(u => u.User.Email == userEmail).Select(a => new BlogsTableItemVM() { Author = a.User.UserName, Published = a.PublishedAt, Header = a.Header, Sport = a.Sport.GetString(), }); var model = new BlogPostsVM() { Sports = sportOptionsList, BlogsTableItems = tableItems, }; return(View(model)); }
public async Task <IActionResult> Results(string SearchString) { var posts = from p in _context.Posts select p; var blogs = _context.Blogs; var tags = _context.Tags; if (!String.IsNullOrEmpty(SearchString)) { posts = posts.Where(p => p.Title.Contains(SearchString) || p.Abstract.Contains(SearchString) || p.Content.Contains(SearchString)); //return View("Index", await posts.Include(p => p.Blog).ToListAsync()); } //return View("Index", await posts.Include(p => p.Blog).ToListAsync()); BlogPostsVM categories = new BlogPostsVM() { Blogs = await blogs.ToListAsync(), Posts = await posts.ToListAsync(), Tags = await tags.ToListAsync() }; return(View("Index", categories)); }
// Model Binding allowed us to send name as a query paramater into the method/action //public string Index(string name, int age) //{ // return $"{name} is {age} years old"; //} public IActionResult Index() { Post post = new Post() { Title = "alice in wonderland", Description = "I give myself very good advice, but i very seldom follow it." }; Blog blog = new Blog { AuthorName = "Lewis Carol", NumberPostsPerWeek = 100 }; BlogPostsVM vm = new BlogPostsVM() { Post = post, Blog = blog }; // cannot send blog since it is not defined in the view. return(View(vm)); }