这个助手输出分页:
@Html.BootstrapPager(
int.Parse( Request.Params[ "page" ] ),
index => Url.Action(
"List",
"Test",
new {
page = index,
amount = 10,
sort = Request.Params[ "sort" ],
order = Request.Params[ "order" ]
}
),
Model.PaginationSet.TotalItemCount,
numberOfLinks: 10
)
BootstrapPager 函数的第二个参数是一个 lambda。 index 变量引用输出页码的内部循环。
有什么方法可以让我预先准备作为 Url.Action 的第三个参数传入的对象,它仍然引用 lambda index 变量?
它可能看起来像这样:
object myActionData = new {
page = <index>, // I don't know how this line would work
amount = 10,
sort = Request.Params[ "sort" ],
order = Request.Params[ "order" ]
}
@Html.BootstrapPager(
int.Parse( Request.Params[ "page" ] ),
index => Url.Action(
"List",
"Test",
myActionData
),
Model.PaginationSet.TotalItemCount,
numberOfLinks: 10
)
请您参考如下方法:
这是不可能的,这里有一个 lambda 的全部意义在于 index 在有效调用 lambda 之前没有设置。
你能做的最好的事情就是事先声明工厂函数。
@{
Func<int, object> myActionDataFactory = index => new {
page = index, // Here we use the parameter
amount = 10,
sort = Request.Params[ "sort" ],
order = Request.Params[ "order" ]
};
}
@Html.BootstrapPager(
int.Parse( Request.Params[ "page" ] ),
index => Url.Action(
"List",
"Test",
myActionDataFactory(index)
),
Model.PaginationSet.TotalItemCount,
numberOfLinks: 10
)
同样,您可以从 BootstrapPager 调用中删除整个 lambda。
@{
Func<int, sting> myUrlFactory = index => Url.Action(
"List",
"Test",
new {
page = index, // Here we use the parameter
amount = 10,
sort = Request.Params[ "sort" ],
order = Request.Params[ "order" ]
});
}
@Html.BootstrapPager(
int.Parse( Request.Params[ "page" ] ),
myUrlFactory,
Model.PaginationSet.TotalItemCount,
numberOfLinks: 10
)
您甚至可以将您的 Url 工厂声明为您在代码中其他地方声明的(可能是静态的)类的方法。
