2015-04-29 124 views
0

父視圖:PagedList MVC HTML輔助,增加鍵/值對HTML輔助

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) 

管窺:

@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 } 
       }) 

我試圖Model.RouteValues添加到局部視圖的HTML PagedListPager的助手。 URL.Action的第二個參數是我需要指定路由值的地方,並且只有「頁面」很好。但是,我試圖找到一種將Model.RouteValues的鍵/值對添加到此參數的方法。

+0

你的意思是這樣的:'@ Html.PagedListPager(型號,頁面=> Url.Action( 「指數」 ,新的{search = ViewBag.Search,page = page}),PagedList.Mvc.PagedListRenderOptions.ClassicPlusFirstAndLast)' –

+0

不,更像是一個可以添加所有值的for循環。因此,如果Model.RouteValues具有「query」/「myQuery」和「name」/「myName」值對,則Url.Action的第二個參數最終會包含頁面,查詢和名稱。 它需要是動態的,以便可以從任何父頁面調用此部分視圖並提供不同的RouteValueDictionary。 – blgrnboy

+0

這可能會幫助你然後http://stackoverflow.com/questions/13533778/mvc-dynamic-routevalues-for-actionlinks –

回答

0

實施了一個工具類和方法,它將「頁面」添加到新的字典中。

實用方法:

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; 
    } 

管窺:

@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 } 
       })