【问题标题】:PagedList MVC Html Helper, add key/value pairs to Html HelperPagedList MVC Html Helper,将键/值对添加到 Html Helper
【发布时间】:2015-04-29 20:23:15
【问题描述】:

父视图:

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 添加到 PagedListPager 的部分视图的 HTML 帮助程序中。 URL.Action 的第二个参数是我需要指定路由值的地方,并且只有“页面”效果很好。但是,我正在尝试找到一种将 Model.RouteValues 的键/值对添加到此参数的方法。

【问题讨论】:

  • 你的意思是这样的:@Html.PagedListPager(Model, page =&gt; Url.Action("Index", new { search = ViewBag.Search, page = page }), PagedList.Mvc.PagedListRenderOptions.ClassicPlusFirstAndLast)
  • 不,更像是一个可以添加所有值的 for 循环。因此,如果 Model.RouteValues 有“query”/“myQuery”和“name”/“myName”值对,则 Url.Action 的第二个参数最终将包含页面、查询和名称。它需要是动态的,以便可以从任何父页面调用此部分视图并提供不同的 RouteValueDictionary。
  • 这可能会对你有所帮助stackoverflow.com/questions/13533778/…
  • 抱歉,这篇文章对我没有帮助。
  • 如果你有Model.RouteValues,你可以通过foreach(var keyValuePair in Model.RouteValues) { extendedRouteValues.Add(keyValuePair.key, keyValuePair.Value) } 遍历这些——我猜,你不能直接访问Model.RouteValues 并且必须使用Request 数组和那个一个似乎没有枚举器,所以键/值对没有 foreach ...

标签: asp.net asp.net-mvc pagedlist url-action


【解决方案1】:

实现了将“页面”添加到新字典的实用程序类和方法。

实用方法:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多