如何在webGrid帮助器中设置总页数

时间:2015-10-12 17:57:19

标签: c# asp.net asp.net-mvc asp.net-mvc-4 webgrid

我使用WebGrid helper mvc来显示数据。我想通过跳过来获取数据并计数这样做我使用此操作:

public static IList<Employee> CreateEmployees(int page,int count)
    {
        var skipe = page * count;
        var list = new List<Employee>();
        var rnd = new Random();
        for (int i = 1; i <= 1000; i++)
        {
            list.Add(new Employee
            {
                Id = i + 1000,
                FirstName = "fName " + i,
                LastName = "lName " + i,
                AddDate = DateTime.Now.AddYears(-rnd.Next(1, 10)),
                Salary = rnd.Next(400, 3000),
                Projects = CreateRandomProjects()
            });
        }

        return list.Skip(skipe).Take(count).ToList();
    }

但是当我在网格视图中显示数据时它只有一个页面,因为我每次都读取10条记录,我想要获取Total页面并将其设置为ViewBag,并将View Set设置为WebGrid中的总计数。我显示Grid的代码是:

    @helper WebGridPageFirstItem(WebGrid grid)
{
    @(((grid.PageIndex + 1) * grid.RowsPerPage) - (grid.RowsPerPage - 1));
}

@helper WebGridPageLastItem(WebGrid grid)
{
    if (grid.TotalRowCount < (grid.PageIndex + 1 * grid.RowsPerPage))
    {
        @grid.TotalRowCount;

    }
    else
    {
        @((grid.PageIndex + 1) * grid.RowsPerPage);
    }
}

<h2>Employees List</h2>

@{
    var grid = new WebGrid(
        source: Model,
        canPage: true,
        rowsPerPage: 10,
        canSort: true,
        defaultSort: "FirstName"
    );
    var salaryPageSum = 0;
    var taxPageSum = 0;
    var rowIndex = ((grid.PageIndex + 1) * grid.RowsPerPage) - (grid.RowsPerPage - 1);
}

<div id="container">
    @grid.GetHtml(
            tableStyle: "webgrid",
            headerStyle: "webgrid-header",
            footerStyle: "webgrid-footer",
            alternatingRowStyle: "webgrid-alternating-row",
            selectedRowStyle: "webgrid-selected-row",
            rowStyle: "webgrid-row-style",
            htmlAttributes: new { id = "MyGrid" },
            mode: WebGridPagerModes.All,
            columns: grid.Columns(
                 grid.Column(header: "#",
                             style: "text-align-center-col",
                             format: @<text>@(rowIndex++)</text>),
                 grid.Column(columnName: "FirstName", header: "First Name",
                             format: @<span style='font-weight: bold'>@item.FirstName</span>,
                             style: "text-align-center-col"),
                 grid.Column(columnName: "LastName", header: "Last Name"),
                 grid.Column(header: "Image",
                             style: "text-align-center-col",
                             format: @<text><img alt="@item.Id" src="@Url.Content("~/images/" + @item.Id + ".jpg")" /></text>),

                 grid.Column(header: "",
                             style: "text-align-center-col",
                             format: item => @Html.ActionLink(linkText: "Edit", actionName: "Edit",
                                                              controllerName: "Home", routeValues: new { id = item.Id },
                                                              htmlAttributes: null)),
                 grid.Column(header: "",
                             format: @<form action="/Home/Delete/@item.Id" method="post">
                    <input type="submit"
                           onclick="return confirm('Do you want to delete this record?');"
                           value="Delete" />
                </form>),
                     grid.Column(header: "", format: item => item.GetSelectLink("Select"))
                          )
                      )

    <strong>Page:</strong> @(grid.PageIndex + 1) / @grid.PageCount,
    <strong>Records:</strong> @WebGridPageFirstItem(@grid) - @WebGridPageLastItem(@grid) of @grid.TotalRowCount

</div>

1 个答案:

答案 0 :(得分:0)

每次调用方法时都会重新创建列表。

你应该这样做:

.
.
var TotalEmployees = 1000;
var list = new List<Employee>();
FillList();

public void FillList(int totalEmployees)
{
   var rnd = new Random();
   for (int i = 1; i <= totalEmployees; i++)
   {
     list.Add(new Employee
     {
       Id = i + 1000,
       FirstName = "fName " + i,
       LastName = "lName " + i,
       AddDate = DateTime.Now.AddYears(-rnd.Next(1, 10)),
       Salary = rnd.Next(400, 3000),
       Projects = CreateRandomProjects()
     });
    }
  }
}


public static IList<Employee> EmployeesForPage(int page,int count)
    {
        var skipe = page * count;
        ViewBag.TotalPages = TotalEmployees/count;
        return list.Skip(skipe).Take(count).ToList();
    }