public static string GetListPageUrl(this WebGrid grid, int pageIndex, dynamic parameters, string actionName) { string controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString(); if (string.IsNullOrEmpty(actionName)) { actionName = "List"; } //int pageNumber = ++pageIndex; string originalURL = grid.GetPageUrl(pageIndex); RouteValueDictionary parms = ParseParameters(originalURL); //parms.Add(grid.PageFieldName, pageNumber); //if (!string.IsNullOrEmpty(grid.SortColumn)) //{ // parms.Add(grid.SortFieldName, grid.SortColumn); // var sortDir = grid.SortDirection == SortDirection.Ascending ? "ASC" : "DESC"; // parms.Add(grid.SortDirectionFieldName, sortDir); //} object obj = parameters; if (obj != null) { var parametersAndValues = from x in obj.GetType().GetProperties() select new { ParamName = x.Name, ParamValue = x.GetValue(obj, null) }; foreach (var item in parametersAndValues) { //if (item.ParamName.ToLower().Equals("actionname")) // actionName = item.ParamValue.ToString(); //else parms.Add(item.ParamName, item.ParamValue); } } var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); var url = urlHelper.Action(actionName, controllerName, parms); return(url); }
private static HelperResult PagerList( WebGrid webGrid, WebGridPagerModes mode, string firstText, string previousText, string nextText, string lastText, int numericLinksCount, string paginationStyle, bool explicitlyCalled) { int currentPage = webGrid.PageIndex; int totalPages = webGrid.PageCount; int lastPage = totalPages - 1; var ul = new TagBuilder("ul"); ul.AddCssClass(paginationStyle); var li = new List <TagBuilder>(); if (webGrid.TotalRowCount <= webGrid.PageCount) { return(new HelperResult(writer => { writer.Write(string.Empty); })); } if (ModeEnabled(mode, WebGridPagerModes.FirstLast)) { if (String.IsNullOrEmpty(firstText)) { firstText = "<<"; } var part = new TagBuilder("li") { InnerHtml = GridLink(webGrid, webGrid.GetPageUrl(0), firstText) }; if (currentPage == 0) { part.MergeAttribute("class", "disabled"); } li.Add(part); } if (ModeEnabled(mode, WebGridPagerModes.NextPrevious)) { if (String.IsNullOrEmpty(previousText)) { previousText = "<"; } int page = currentPage == 0 ? 0 : currentPage - 1; var part = new TagBuilder("li") { InnerHtml = GridLink(webGrid, webGrid.GetPageUrl(page), previousText) }; if (currentPage == 0) { part.MergeAttribute("class", "disabled"); } li.Add(part); } if (ModeEnabled(mode, WebGridPagerModes.Numeric) && (totalPages > 1)) { int last = currentPage + (numericLinksCount / 2); int first = last - numericLinksCount + 1; if (last > lastPage) { first -= last - lastPage; last = lastPage; } if (first < 0) { last = Math.Min(last + (0 - first), lastPage); first = 0; } for (int i = first; i <= last; i++) { var pageText = (i + 1).ToString(CultureInfo.InvariantCulture); var part = new TagBuilder("li") { InnerHtml = GridLink(webGrid, webGrid.GetPageUrl(i), pageText) }; if (i == currentPage) { part.MergeAttribute("class", "active"); } li.Add(part); } } if (ModeEnabled(mode, WebGridPagerModes.NextPrevious)) { if (String.IsNullOrEmpty(nextText)) { nextText = ">"; } int page = currentPage == lastPage ? lastPage : currentPage + 1; var part = new TagBuilder("li") { InnerHtml = GridLink(webGrid, webGrid.GetPageUrl(page), nextText) }; if (currentPage == lastPage) { part.MergeAttribute("class", "disabled"); } li.Add(part); } if (ModeEnabled(mode, WebGridPagerModes.FirstLast)) { if (String.IsNullOrEmpty(lastText)) { lastText = ">>"; } var part = new TagBuilder("li") { InnerHtml = GridLink(webGrid, webGrid.GetPageUrl(lastPage), lastText) }; if (currentPage == lastPage) { part.MergeAttribute("class", "disabled"); } li.Add(part); } ul.InnerHtml = string.Join("", li); var html = ""; if (explicitlyCalled && webGrid.IsAjaxEnabled) { var span = new TagBuilder("span"); span.MergeAttribute("data-swhgajax", "true"); span.MergeAttribute("data-swhgcontainer", webGrid.AjaxUpdateContainerId); span.MergeAttribute("data-swhgcallback", webGrid.AjaxUpdateCallback); span.InnerHtml = ul.ToString(); html = span.ToString(); } else { html = ul.ToString(); } return(new HelperResult(writer => { writer.Write(html); })); }