2015-04-29 128 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 } 
       })