0

我正在从存储过程返回记录,并且我想在视图中使用自定义分页。这是我到目前为止所拥有的:

控制器:

        public ActionResult Index(int currentPage=1, int PageNo = 1, int PageSize = 10, string SortColumn = "Name", string SortOrder = "Asc", string SearchString = "", int totalRecords=0)
    {
        DataContextDataContext obj = new DataContextDataContext();

        System.Nullable<int> Total = null;

        //PageCount = (int)Math.Ceiling((double)Total / PageSize);
        var model = obj.TempItemSubClassList(PageNo, PageSize, SortColumn, SortOrder, SearchString, ref Total).ToList();

        int PageCount = (int)(Total + PageSize - 1) / PageSize;


        StringBuilder sb1 = new StringBuilder();

        int seed = currentPage % PageSize == 0 ? currentPage : currentPage - (currentPage % PageSize);

        if (currentPage > 0)
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Previous</a>", SearchString, currentPage));

        if (currentPage - PageSize >= 0)
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", SearchString, (currentPage - PageSize) + 1));

        for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + PageSize; i++)
        {
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">{1}</a>", SearchString, i + 1));
        }

        if (currentPage + PageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", SearchString, (currentPage + PageSize) + 1));

        if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Next</a>", SearchString, currentPage + 2));

        //Response.Write(sb1);////can/should I append this to the model?

        return View(model);
}

看法:

@model IEnumerable<ArtGuildMVC2.Models.TempItemSubClassListResult>

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model.ItemClassId)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Description)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
   <tr>
    <td>
        @Html.DisplayFor(modelItem => item.ItemClassId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Description)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
 }
 </table>

/////What do I do hereafter????

 @{
 if (ViewBag.currentPage > 1) {
<a href="@Url.Action("index",new {page=1})">First</a>
<a href="@Url.Action("index",new {page=ViewBag.currentPage-1})">Prev</a>
}
if (ViewBag.currentPage < ViewBag.Total)
{
 <a href="@Url.Action("index",new {page=ViewBag.currentPage+1})">Next</a>
 <a href="@Url.Action("index",new {page=ViewBag.Total})">Last</a>
}}

如何实现视图中的分页逻辑?提前致谢。PS:你可能觉得代码不太合乎逻辑,因为我从 2-3 个地方挑选了它,并试图让它在试错的基础上工作。

4

1 回答 1

1

我会说您将通过您在 Action 方法中编写的代码来打破 MVC 模式。

顺便说一句,分页是一些库(如MvcPaging )解决的问题,所以我强烈建议您使用其中之一。

于 2013-09-04T14:27:53.873 回答