public IActionResult Index(string filter, int pageindex = 1, string sortExpression = nameof(DetailsNewsViewModel.Title)) { IQueryable <News> newsDbCollection = this.newsService.GetAll(); if (!string.IsNullOrWhiteSpace(filter)) { newsDbCollection = newsDbCollection.Where(x => x.Title.Contains(filter)); } List <DetailsNewsViewModel> detailsNewsViewModels = new List <DetailsNewsViewModel>(); foreach (News newsDb in newsDbCollection.ToList()) { DetailsNewsViewModel listNewsViewModel = new DetailsNewsViewModel { Id = newsDb.Id, Title = newsDb.Title, Content = newsDb.Content, ReporterName = newsDb.User.UserName, CreatedAt = newsDb.CreatedAt, UpdatedAt = newsDb.UpdatedAt, ReporterId = newsDb.UserId }; detailsNewsViewModels.Add(listNewsViewModel); } PagingList <DetailsNewsViewModel> pagingListViewModels = PagingList.Create(detailsNewsViewModels, 3, pageindex, sortExpression, nameof(DetailsNewsViewModel.Title)); pagingListViewModels.RouteValue = new RouteValueDictionary { { "filter", filter } }; return(View(pagingListViewModels)); }
public IActionResult GetDetails(string id) { News newsDb = newsService.GetAll(x => x.Id == id).FirstOrDefault(); if (newsDb == null) { ModelState.AddModelError("", "A news with this id doesn't exist!"); return(View("ValidationErrorsWithoutSpecificModel")); } DetailsNewsViewModel detailsNewsViewModel = new DetailsNewsViewModel { Id = newsDb.Id, Content = newsDb.Content, Title = newsDb.Title, ReporterName = newsDb.User.UserName, CreatedAt = newsDb.CreatedAt, UpdatedAt = newsDb.UpdatedAt, }; return(View(detailsNewsViewModel)); }