asp.net-mvc-3 – 我可以将PartialViewResult与PagedList一起使用并在PartialView中显示它

大家好我有一个问题,我可以在PartialViewResult Action中使用PagedList并在PartialView中显示结果吗?

这是一些代码

控制器代码:

public PartialViewResult CargosPorProyecto(string id, int? page)
    {
        var cargos = db.Cargo.Include(i => i.Proyectos).Where(i => i.NumProyecto.Equals(id)).OrderByDescending(i => i.Fecha);

        if (Request.HttpMethod != "GET")
        {
            page = 1;
        }

        var pageSize = 10;
        var pageNumber = (page ?? 1);
        var onePage = cargos.ToPagedList(pageNumber, pageSize);


        return PartialView("ListaCargosParcial", ViewBag.OnePage = onePage);
    }

在我的PartialView中,我将此代码显示为分页

<div class="pagination-right">
    <div class="span12">
       <%: Html.PagedListPager((IPagedList)ViewBag.OnePage, page => Url.Action("CargosPorProyecto", new { page = page }), new PagedListRenderOptions { LinkToFirstPageFormat = "<< Primera", LinkToPreviousPageFormat = "< Anterior", LinkToNextPageFormat = "Siguiente >", LinkToLastPageFormat = "&Uacute;ltima >>" })%> 
    </div>                    
    </div>

当我加载包含partialview的页面时,一切看起来都不错,但是当我单击Next(“Siguiente”)时,我的局部视图中没有加载.

我希望我能清楚地解释并感谢你的时间.

问候

最佳答案 如果你想留在同一页面,你可以使用AJAX.例如,如果您使用的是jQuery,则可以订阅分页链接的click事件,然后触发对相应控制器操作的AJAX请求,并使用返回的结果刷新部分:

$(function() {
    $('.pagination-right a').click(function() {
        $.ajax({
            url: this.href,
            type: 'GET',
            cache: false,
            success: function(result) {
                // refresh the contents of some container div for the partial
                // make sure you use the correct selector here
                $('#some_container_for_the_partial').html(result);
            }
        });

        // cancel the default action which is a redirect
        return false;
    });
});
点赞