我的留言簿视图中的asp.net mvc 5分页

时间:2022-10-19 04:04:47

I'm just learning MVC 5 and i want to create a paging after 10 comments have been posted on the page. The code is very simple so far.

我刚学习MVC 5,我想在页面上发布10条评论后创建一个分页。到目前为止,代码非常简单。

Here is my Controller:

这是我的控制器:

    public ActionResult Index()
    {
        var Inlägg = db.Gästbok.OrderByDescending(Gästbok => Gästbok.Datum).Take(10).ToList();

        return View(Inlägg);
    }

Here is the View:

这是视图:

        @foreach (var item in Model)
        {
            <p>@Html.DisplayFor(modelItem => item.Kommentar)</p>
            <br />

            <p>Postad: @Html.DisplayFor(modelItem => item.Datum) Av: @Html.DisplayFor(modelItem => item.Namn) (@Html.DisplayFor(modelItem => item.Email))</p>

            <hr />
        }

        <button class="btn btn-default">
            @Html.ActionLink("Skriv nytt inlägg", "Create")
        </button>

So if anyone can give me a simple example or explanation to how i can implement a pagination here I would really appriciate it! Thanks in advance!! :D

所以,如果有人能给我一个简单的例子或解释我如何在这里实现分页,我真的很喜欢它!提前致谢!! :d

1 个解决方案

#1


0  

I can recommend installing the PagedList.MVC:

我可以推荐安装PagedList.MVC:

Install-Package PagedList 

Then implement something like this:

然后实现这样的事情:

Controller:

public ActionResult Index(int page = 1, int pageSize = 10)
{
    var inlägg = db.Gästbok.OrderByDescending(g => g.Datum).ToList();
    var model = new PagedList<Gästbok>(inlägg, page, pageSize);

    return View(model);
}

View:

@model PagedList<Gästbok>

@foreach (var item in Model)
{
    <p>@Html.DisplayFor(modelItem => item.Kommentar)</p>
    <br />

    <p>
        Postad: @Html.DisplayFor(modelItem => item.Datum) 
        Av: @Html.DisplayFor(modelItem => item.Namn
        (@Html.DisplayFor(modelItem => item.Email))
    </p>

    <hr />
}

<p>
    @Html.PagedListPager(Model, page => Url.Action("Index",
        new { page, pageSize = Model.PageSize }))

    Showing @Model.FirstItemOnPage to @Model.LastItemOnPage
        of @Model.TotalItemCount comments.
</p>
<hr />

<button class="btn btn-default">
    @Html.ActionLink("Skriv nytt inlägg", "Create")
</button>

And add the PagedList dlls to the <namespaces> element in the <system.web.webPages.razor> section under <configuration> in the Views/Web.config as follows:

并将PagedList dll添加到Views / Web.config中 下的 部分中的 元素,如下所示:

<configuration>
  (...)
  <system.web.webPages.razor>
    (...)
    <namespaces>
      (...)
      <add namespace="PagedList" />
      <add namespace="PagedList.Mvc" />
    </namespaces>
  </system.web.webPages.razor>
</configuration>

It will look something like this:

它看起来像这样:

我的留言簿视图中的asp.net mvc 5分页

#1


0  

I can recommend installing the PagedList.MVC:

我可以推荐安装PagedList.MVC:

Install-Package PagedList 

Then implement something like this:

然后实现这样的事情:

Controller:

public ActionResult Index(int page = 1, int pageSize = 10)
{
    var inlägg = db.Gästbok.OrderByDescending(g => g.Datum).ToList();
    var model = new PagedList<Gästbok>(inlägg, page, pageSize);

    return View(model);
}

View:

@model PagedList<Gästbok>

@foreach (var item in Model)
{
    <p>@Html.DisplayFor(modelItem => item.Kommentar)</p>
    <br />

    <p>
        Postad: @Html.DisplayFor(modelItem => item.Datum) 
        Av: @Html.DisplayFor(modelItem => item.Namn
        (@Html.DisplayFor(modelItem => item.Email))
    </p>

    <hr />
}

<p>
    @Html.PagedListPager(Model, page => Url.Action("Index",
        new { page, pageSize = Model.PageSize }))

    Showing @Model.FirstItemOnPage to @Model.LastItemOnPage
        of @Model.TotalItemCount comments.
</p>
<hr />

<button class="btn btn-default">
    @Html.ActionLink("Skriv nytt inlägg", "Create")
</button>

And add the PagedList dlls to the <namespaces> element in the <system.web.webPages.razor> section under <configuration> in the Views/Web.config as follows:

并将PagedList dll添加到Views / Web.config中 下的 部分中的 元素,如下所示:

<configuration>
  (...)
  <system.web.webPages.razor>
    (...)
    <namespaces>
      (...)
      <add namespace="PagedList" />
      <add namespace="PagedList.Mvc" />
    </namespaces>
  </system.web.webPages.razor>
</configuration>

It will look something like this:

它看起来像这样:

我的留言簿视图中的asp.net mvc 5分页