How do I add a querystring when using the MVC PagedList?

柔情痞子 提交于 2019-12-14 04:08:07

问题


How would I go about adding a query string to the Razor Helper PagedListPager below ?

@Html.PagedListPager( (IPagedList)Model.PartsList, page => Url.Action("Index", new { page }) )

回答1:


You don't add query parameters in PagedListPager, you do them in Url.Action.

Below is an updated version of your code, I've added a query parameter tag.

Url.Action("Index", new { page, tag = 'asp' }) 

The URL would generate the following query string

?page=1&tag=asp

Here is the same Url.Action within PagedListPager:

@Html.PagedListPager(
    (IPagedList)Model.PartsList, 
    page => Url.Action("Index", new { page, tag = 'asp' }))



回答2:


You simply add more query parameters

Url.Action("Index", new { page, tag = "asp", tag1 = "value1", tag2 = "value2" })

The URL would generate the following query string

?page=1&tag=asp&tag1=value1&tag2=value2

If a value is null the query string is not included in generated URL

Url.Action("Index", new { page, tag = "asp", tag1 = "value1", tag2 = "" })

?page=1&tag=asp&tag1=value1

Many thanks to Yorro!



来源:https://stackoverflow.com/questions/23463685/how-do-i-add-a-querystring-when-using-the-mvc-pagedlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!