在 lambda 定义之前准备一个引用 lambda 变量的对象
Preparing an object that references a lambda variable before lambda even defined
这个助手输出分页:
@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 工厂声明为您在代码中其他地方声明的(可能是静态的)class 的方法。
这个助手输出分页:
@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 工厂声明为您在代码中其他地方声明的(可能是静态的)class 的方法。