Asp.net mvc 4 MVC4视图中带有存储过程的自定义分页逻辑

Asp.net mvc 4 MVC4视图中带有存储过程的自定义分页逻辑,asp.net-mvc-4,custompaging,Asp.net Mvc 4,Custompaging,我正在从存储过程返回记录,我想在视图中使用自定义分页。这就是我到目前为止所做的: 控制器: public ActionResult Index(int currentPage=1, int PageNo = 1, int PageSize = 10, string SortColumn = "Name", string SortOrder = "Asc", string SearchString = "", int totalRecords=0) { Da

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

控制器:

        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);
}
public ActionResult索引(int-currentPage=1,int-PageNo=1,int-PageSize=10,string-SortColumn=“Name”,string-SortOrder=“Asc”,string-SearchString=”,int-totalRecords=0)
{
DataContextDataContext obj=新建DataContextDataContext();
System.Nullable Total=null;
//PageCount=(int)数学上限((双)总计/页面大小);
var model=obj.TempItemSubClassList(PageNo、PageSize、SortColumn、SortOrder、SearchString、ref Total).ToList();
int PageCount=(int)(总数+PageSize-1)/PageSize;
StringBuilder sb1=新的StringBuilder();
int seed=currentPage%PageSize==0?currentPage:currentPage-(currentPage%PageSize);
如果(当前页面>0)
sb1.AppendLine(String.Format(“,SearchString,currentPage));
如果(当前页面-页面大小>=0)
sb1.AppendLine(String.Format(“),SearchString,(currentPage-PageSize)+1);
对于(inti=seed;i如果(currentPage+PageSize,我会说您将通过在操作方法中编写的代码来打破MVC模式

顺便说一下,分页是一些库解决的问题,我强烈建议您使用其中的一个

@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>
}}