blgrnboy
blgrnboy

Reputation: 5157

PagedList MVC Html Helper, add key/value pairs to Html Helper

Parent View:

var pagingModel = new Watchlist_Web.Models.ViewModel.PagingPartialViewModel();
                pagingModel.PagedList = Model;
                pagingModel.UrlAction = "AdminIndex";

                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("query", Request.QueryString["query"]);
                parameters.Add("modelClass", Request.QueryString["nodeClassId"]);

                pagingModel.RouteValues = new RouteValueDictionary(parameters);

                pagingModel.ContainerDivClasses = "pagination-sm col-md-5";
@Html.Partial("_PagingPartial", pagingModel)

Partial View:

@using PagedList;
@using PagedList.Mvc;

@model Watchlist_Web.Models.ViewModel.PagingPartialViewModel
@Html.PagedListPager(Model.PagedList, page => Url.Action(Model.UrlAction,
        new RouteValueDictionary(new { page = page })),
        new PagedListRenderOptions()
                {
                    DisplayPageCountAndCurrentLocation = true,
                    DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded,
                    DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded,
                    ContainerDivClasses = new[] { Model.ContainerDivClasses }
                })

I am attempting to add Model.RouteValues to the partial view's HTML Helper for PagedListPager. The second parameter for URL.Action is where I need to specify my route values, and having only "page" works great. However, I am trying to find a way to add the key/value pairs of Model.RouteValues to this parameter.

Upvotes: 0

Views: 1534

Answers (1)

blgrnboy
blgrnboy

Reputation: 5157

Implemented a utilities Class and Method that adds "page" to a new dictionary.

Utility Method:

public static RouteValueDictionary GetPagingRouteValDictionary(int page, RouteValueDictionary dict)
    {
        if (dict["page"] != null)
        {
            dict.Remove("page");
        }

        var newDict = new RouteValueDictionary(dict);
        newDict.Add("page", page);

        return newDict;
    }

Partial View:

@Html.PagedListPager(Model.PagedList, page => Url.Action(Model.UrlAction,
    Watchlist_Web.Utility.RouteValueDictUtil.GetPagingRouteValDictionary(page, Model.RouteValues)),

    new PagedListRenderOptions()
                {
                    DisplayPageCountAndCurrentLocation = true,
                    DisplayLinkToFirstPage = PagedListDisplayMode.IfNeeded,
                    DisplayLinkToLastPage = PagedListDisplayMode.IfNeeded,
                    ContainerDivClasses = new[] { Model.ContainerDivClasses }
                })

Upvotes: 1

Related Questions