public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { var result = new StringBuilder(); if (pagingInfo.TotalPages <= 1) return MvcHtmlString.Empty; for (int i = pagingInfo.CurrentPage; i <= (5 + pagingInfo.CurrentPage); i++) { int startIndex = (i < 5) ? 1 : (pagingInfo.CurrentPage - 5) + 2; int endIndex = (i < 5) ? pagingInfo.TotalPages : (pagingInfo.CurrentPage + 2) <= pagingInfo.TotalPages ? (pagingInfo.CurrentPage + 2) : pagingInfo.TotalPages; bool frontArrows = (i - 5 >= 0); bool endArrows = (pagingInfo.TotalPages - (i + 2)) > 0; if (frontArrows) result.Append("... "); for (int j = startIndex; j <= endIndex; j++) { var tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(j)); tag.InnerHtml = j.ToString(CultureInfo.InvariantCulture); if (j == pagingInfo.CurrentPage) { tag.AddCssClass("selected-page-link"); tag.Attributes.Add("style", "color:blue;"); } result.Append(tag.ToString()); if (i != pagingInfo.TotalPages) result.Append(" "); } if (endArrows) result.Append("..."); break; } return MvcHtmlString.Create(result.ToString()); }
private List<SnippetDetailsModel> MergeUserAndSnippets(List<SnippetDetailsModel> viewModel, PagingInfo pagingInfo) { var newList = new List<SnippetDetailsModel>(); if (viewModel != null && viewModel.Any()) { newList.AddRange(from snippetDTO in viewModel let currentUser = _managerService.UserDetails(snippetDTO.UserId, snippetDTO.UserGuid) select new SnippetDetailsModel { SnippetId = snippetDTO.SnippetId, Name = snippetDTO.Name, Description = snippetDTO.Description, SnippetUrl = snippetDTO.SnippetId.ToString(), PreviewData = snippetDTO.PreviewData, UserName = (currentUser != null && currentUser.Id > 0) ? currentUser.LoginName : MvcApplication.AnonoymousUserName, UserAvatar = (currentUser != null && currentUser.Id > 0) ? currentUser.AvatarImage : null, UserId = (currentUser.Id > 0) ? currentUser.Id : MvcApplication.AnonymousUserId, UserGuid = (currentUser.FormsAuthId != Guid.Empty) ? currentUser.FormsAuthId : MvcApplication.AnonymousUserGuid, IsPublic = snippetDTO.IsPublic, PagingInfo = pagingInfo }); } return newList; }
private IEnumerable<SnippetDetailsModel> ConvertDtoCollectionToModelCollection(IEnumerable<SnippetDTO> response, PagingInfo pagingInfo) { var results = new List<SnippetDetailsModel>(); results.AddRange( from snippetDTO in response let currentUser = _managerService.UserDetails(snippetDTO.User_Id, snippetDTO.User_FormsAuthId) select new SnippetDetailsModel { SnippetId = snippetDTO.Guid, Name = snippetDTO.Name, Description = snippetDTO.Description, SnippetUrl = snippetDTO.Guid.ToString(), PreviewData = Encoding.UTF8.GetString(snippetDTO.PreviewData), UserName = (currentUser != null && currentUser.Id > 0) ? currentUser.LoginName : MvcApplication.AnonoymousUserName, UserAvatar = (currentUser != null && currentUser.Id > 0) ? currentUser.AvatarImage : new byte[0], UserId = (currentUser.Id > 0) ? currentUser.Id : MvcApplication.AnonymousUserId, UserGuid = (currentUser.FormsAuthId != Guid.Empty) ? currentUser.FormsAuthId : MvcApplication.AnonymousUserGuid, IsPublic = snippetDTO.IsPublic, PagingInfo = pagingInfo }); return results; }
public ActionResult SearchResults(int page = 1) { string searchText = string.Empty; var searchResults = new List<SnippetDetailsModel>(); if (Session["SearchTerm"] != null && !string.IsNullOrEmpty(Session["SearchTerm"].ToString())) { searchText = Session["SearchTerm"].ToString(); } if (TempData["SearchResults"] != null) { searchResults = ((List<SnippetDetailsModel>) TempData["SearchResults"]); } var viewModel = new List<SnippetDetailsModel>(); var pagingInfo = new PagingInfo { ItemsPerPage = MAX_SNIPPETS_PER_PAGE, CurrentPage = page, TotalItems = searchResults != null ? searchResults.Count() : 0 }; if (searchResults != null) { viewModel = ((List<SnippetDetailsModel>) TempData["SearchResults"]); if (viewModel != null && viewModel.Any()) { viewModel = MergeUserAndSnippets(viewModel, pagingInfo); } else { TempData["Message"] = "No results found"; } } else { if (!string.IsNullOrEmpty(searchText)) viewModel = (List<SnippetDetailsModel>) ConvertDtoCollectionToModelCollection( _managerService.FindSnippetsPaged((searchText == string.Empty) ? string.Empty : searchText, pagingInfo.ItemsPerPage, page), pagingInfo); } TempData["SearchTerm"] = searchText; return View(viewModel); }
public ActionResult PerformSearch(string searchText, int page = 1) { if (string.IsNullOrEmpty(searchText)) return RedirectToAction("SearchResults", new {results = new List<SnippetDetailsModel>()}); var results = new List<SnippetDetailsModel>(); var pagingInfo = new PagingInfo {CurrentPage = page, ItemsPerPage = MAX_SNIPPETS_PER_PAGE}; try { // Get search results SnippetDTO[] response = _managerService.FindSnippetsPaged(searchText ?? string.Empty, MAX_SNIPPETS_PER_PAGE, page); pagingInfo.TotalItems = _managerService.FindSnippets(searchText).Count(); if (response.Any()) { results = ConvertDtoCollectionToModelCollection(response, pagingInfo).ToList(); } } catch (Exception ex) { Logger.LogError("PerformSearch exception", ex); TempData["Error"] = "Unknow error occurred."; return RedirectToAction("All"); } TempData["SearchResults"] = results; Session["SearchTerm"] = (string.IsNullOrEmpty(searchText) ? string.Empty : searchText); return RedirectToAction("SearchResults", new {results = results.ToArray()}); }
public ActionResult Index(int page = 1) { var viewModel = new List<SnippetDetailsModel>(); var pagingInfo = new PagingInfo {CurrentPage = page}; UserDTO user = GetCurrentUserData(); //User.Identity as CustomIdentity; try { int currentCount = _managerService.GetTotalSnippetCount(false); pagingInfo.TotalItems = currentCount; if (user != null) { IEnumerable<SnippetDTO> snippetList = _managerService.GetAllPublicSnippets(MAX_SNIPPETS_PER_PAGE, page).OrderByDescending( d => d.LastModified); if (snippetList.Any()) { viewModel.AddRange( from snippetDTO in snippetList let snippetsOwner = _managerService.UserDetails(snippetDTO.User_Id, snippetDTO.User_FormsAuthId) select new SnippetDetailsModel { Name = snippetDTO.Name, Description = snippetDTO.Description, SnippetUrl = snippetDTO.Guid.ToString(), PreviewData = Encoding.UTF8.GetString(snippetDTO.PreviewData), UserName = snippetsOwner.LoginName, UserAvatar = snippetsOwner.AvatarImage, UserId = snippetsOwner.Id, UserGuid = snippetsOwner.FormsAuthId, IsPublic = snippetDTO.IsPublic, PagingInfo = pagingInfo }); } } } catch (Exception ex) { ModelState.AddModelError("IndexError", ex); ViewBag.Message = ex.Message; return View(viewModel); } return View(viewModel); }