0

我在显示有关如何包含上一个和下一个链接的页面时遇到问题...我正在使用 ASP.Net MVC 4 使用这些代码:

看法:

<div class="pagination">
    <ul>
        @for (int i = 1; i <= ViewBag.PagesCount; i++)
        {
            <text>
                <li>@Html.ActionLink(i.ToString(), "Index", new { pagee = i })</li>
            </text>
        }
    </ul>
</div>

控制器:

int offset = 15;

int pagecount = ((pagee - 1) * offset);

int totalPages = 0;
int totalItems = 0;

try
{       
    totalItems = requestform.Count();
    while (totalItems > 0)
    {
        totalItems -= 15;
        totalPages++;
    }
    ViewBag.PagesCount = totalPages;
    return View(requestform.Skip(pagecount).Take(offset).ToList());
}
catch
{
    while (totalItems > 0)
    {
        totalItems -= 15;
        totalPages++;
    }
    ViewBag.PagesCount = totalPages;
    return View(requestform.Skip(pagecount).Take(offset).ToList());
}

请帮我解决一下这个。

4

4 回答 4

2

你对这个小扩展PagedList有什么看法?

于 2013-03-18T07:08:56.780 回答
0

我还建议 PagedList 只需通过 NuGet 包管理器添加它,然后,

修改你的控制器看起来像这样:

public ActionResult Index(int? page)
        {
            var dbtable = db.DbTable.Include(s => s.ID).OrderBy(s => s.StyleName); //ensure records are sorted.
            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }
            int pageSize = 2;
            int pageNumber = (page ?? 1);
            return View(dbtable.ToPagedList(pageNumber, pageSize));
        }

鉴于更换:

@model IEnumerable<MyMvcApplication.Models.dbtable>

@model PagedList.IPagedList<MyMvcApplication.Models.dbtable>

替换看起来像的代码

    <th>
            @Html.DisplayNameFor(model => model.ID)
        </th>

<th>
            @Html.DisplayNameFor(model => model.First().ID)
        </th>

</table>在标签后的页面末尾添加以下内容:

<div>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "Index", new { page = 1})
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "Index", new {page = Model.PageNumber - 1})
    }
    else{
     @:<<
     @Html.Raw(" ");
        @:< Prev   
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "Index", new {page = Model.PageNumber + 1})
        @Html.Raw(" "); 
        @Html.ActionLink(">>", "Index", new {page = Model.PageCount})
    }
    else{
     @:Next >
     @Html.Raw(" ")
@:>>   
    }
</div>
于 2013-06-10T14:47:48.163 回答
0

To add paging to page, you'll start by installing the PagedList NuGet package. Then you'll make additional changes in the Index method and add paging links to the Index view

Please check this link http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

于 2013-03-18T07:04:09.183 回答
0

最近我发现了很酷的控件,工具提示,第一个,下一个,最后一个,上一个检查这个mvc 4 分页 nuget 包,演示网页

于 2013-06-17T11:38:00.120 回答