Razor MVC partialview pagination url action, How to define specified url?

三世轮回 提交于 2019-12-10 18:52:42

问题


Default url: http://ipf.bulgaria.local/Aboutus.aspx

After click the pagination URL: http://ipf.bulgaria.local/api/sitecore/Newsfeed?page=2

Expected URL: http://ipf.bulgaria.local/Aboutus.aspx?page=2

Can someone advice me how to achieve the expected url and how to get read of "/api/sitecore/Newsfeed"?

<div class="col-sm-10">
    <div class="center">        
            @Html.PagedListPager((PagedList.IPagedList)Model
            .SelectedNewsItems, 
            page => Url.Action("Index", new { page }))
    </div>
</div>

回答1:


It is about the MVC area.

This is described in the following blog: http://sitecore.unic.com/2015/02/04/get-url-for-area-controller-action-from-a-view-rendering




回答2:


Default url: http://ipf.bulgaria.local/Aboutus.aspx

After click the pagination URL: http://ipf.bulgaria.local/api/sitecore/Newsfeed?page=2

Expected URL: http://ipf.bulgaria.local/Aboutus.aspx?page=2

Can someone advice me how to achieve the expected url and how to get read of "/api/sitecore/Newsfeed"?

Basically what you need is the same URL always after clicking on the different pages with page number being shown as query string.

So the first thing you need to do is pass model as

@model PagedList.IPagedList<Type>

So from you controller you would pass the correct set of data based on page number as:

YourList.ToPagedList(pageNumber, pageSize);

Hence every time you need to pass pagenumber to controller so that the respective page data is passed to the view which would be relevant. The question is how would i know page number? The paged list control gives you page number and you would use this to pass it as query string and read in controller. so your problem of URL will be solved. Hope this helps and for better understanding please see video: https://www.youtube.com/watch?v=6bKZoKyBlE8




回答3:


Even though it has been a while this question is asked, it may be useful for those who are still having an issue with the generated pagination URL's. What you are getting is the default route. To solve it, you can add a custom route and point to it in the code @Html.PagedListPager.

But in your case the following will do the trick:

@Html.PagedListPager(Model, page => 
LinkManager.GetItemUrl(Sitecore.Context.Item) + "?page=" + page)

The part LinkManager.GetItemUrl(Sitecore.Context.Item) will get the URL of the current item. This way you will get "Aboutus.aspx?page=2" URL as expected.



来源:https://stackoverflow.com/questions/30467064/razor-mvc-partialview-pagination-url-action-how-to-define-specified-url

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