MVC 3:在 ActionLink 上附加获取参数
我正在使用 MVCContrib 网格输出一些数据。当我对列进行排序时,我得到一个可能如下所示的网址:
/?Column=ColumnName&Direction=Ascending
假设我想添加链接来控制显示的结果数量。我会自发地写出这样的内容:
Html.ActionLink("View 10", "Index", new { pageSize = 10 })
...这会给我:
/?PageSize=10
但是假设我已经对网格进行了排序。在这种情况下,我想保存 url 参数,使新的 url 看起来像这样:
/?Column=ColumnName&Direction=Ascending&PageSize=10
如何完成此操作?
I'm using the MVCContrib grid to output some data. When I sort a column, I get a url which may look like this:
/?Column=ColumnName&Direction=Ascending
Lets say I want to add links to control how many results are being shown. Spontaneously I would write something like this:
Html.ActionLink("View 10", "Index", new { pageSize = 10 })
... which would give me:
/?PageSize=10
But say I already sorted the grid. In that case I want to save the url parameters, making the new url look something like this:
/?Column=ColumnName&Direction=Ascending&PageSize=10
How can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在生成链接时包含这些其他参数:
或者编写一个自定义 html 帮助程序,它将自动包含所有当前查询字符串参数并附加
pageSize
参数:该帮助程序如下所示:
You could include those other parameters when generating the link:
or write a custom html helper which will automatically include all current query string parameters and append the
pageSize
parameter:and here's how the helper could look like:
您还可以使用问题 ASP.NET MVC:通过所有 ActionLink 传播查询参数的正确方法。它将允许您保留您喜欢的任何路线上的任何参数。
You can also use solution from question ASP.NET MVC: The right way to propagate query parameter through all ActionLinks. It will allow you to preserve any parameters on any routes you like.