How do I add a querystring when using the MVC Page

2019-08-30 01:51发布

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

2条回答
神经病院院长
2楼-- · 2019-08-30 02:27

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' }))
查看更多
Root(大扎)
3楼-- · 2019-08-30 02:49

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!

查看更多
登录 后发表回答