0

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.

blgrnboy
  • 4,877
  • 10
  • 43
  • 94
  • You mean like this: `@Html.PagedListPager(Model, page => Url.Action("Index", new { search = ViewBag.Search, page = page }), PagedList.Mvc.PagedListRenderOptions.ClassicPlusFirstAndLast)` – Bikal Bhattarai Apr 29 '15 at 20:38
  • No, more like a for loop that can add all of values. So, if Model.RouteValues had "query"/"myQuery" and "name"/"myName" value pairs, the second param of Url.Action would ultimate have page, query, and name in it. It needs to be dynamic so that this partial view can be called from any parent page and provide a different RouteValueDictionary. – blgrnboy Apr 29 '15 at 20:44
  • This might help you then http://stackoverflow.com/questions/13533778/mvc-dynamic-routevalues-for-actionlinks – Bikal Bhattarai Apr 29 '15 at 20:49
  • Sorry, that post doesn't really help me. – blgrnboy Apr 29 '15 at 21:15
  • If you have `Model.RouteValues` you can iterate over these by `foreach(var keyValuePair in Model.RouteValues) { extendedRouteValues.Add(keyValuePair.key, keyValuePair.Value) }` - I guess, you don't have direct access to `Model.RouteValues` and have to live with the `Request` array and that one doesn't seem to have an enumerator, so there's no foreach for the key/value pairs ... – outofmind Dec 07 '15 at 16:39

1 Answers1

1

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 }
                })
blgrnboy
  • 4,877
  • 10
  • 43
  • 94