2

我有以下代码,我有一个如下所示的页面: 页面片段

我希望能够选择每页的行数,但我不断收到一条错误消息:“CS1502:'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, string, int, bool, bool, string, string, string, string, string, string, string)' 有一些无效参数”

当 var rpp = data.Count() 时,网格 1 实际上采用了 @rpp 的剃刀语法,但是当我在网格 2 中使用 @rpp 并使 var rpp = Request["rpp1'] 它没有采用它时,它给了我错误。有什么办法可以避免这种情况?

@{
    if(Request["action"] == "Go")
    {
    var rpp = Request["rpp1"];

    <p>Showing @rpp rows per page.</p> 
    var columns2 = new[]{"ID", "SSO", "Category", "System", "Subject", "Created", "Assigned_To", "Case_Status"};
    var grid2 = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns2, rowsPerPage: @rpp);
    if (Request.QueryString[grid.SortDirectionFieldName].IsEmpty()) {
    grid2.SortDirection = SortDirection.Descending;
    }
    @grid2.GetHtml(    
        tableStyle : "table",
        alternatingRowStyle : "alternate",
        headerStyle : "header",
        mode: WebGridPagerModes. All,
        previousText: "Previous",
        nextText: "Next",
        firstText: "First Page",
        lastText: "Last Page",
        columns: grid.Columns(
                     grid.Column("ID", "ID"),
                     grid.Column("SSO", "SSO"),
                     grid.Column("Category", "Category"),
                     grid.Column("System", "System"),
                     grid.Column("Subject", "Subject"),
                     grid.Column("Created", "Created"),
                     grid.Column("Assigned_To", "Assigned To"),
                     grid.Column("Case_Status", "Status")
    )
    )
   } else{

    if(Request["action"] == "Plain Table - Excel")
    {
    var rpp = data.Count();
    var columns1 = new[]{"ID", "SSO", "Category", "System", "Subject", "Created", "Assigned_To", "Case_Status"};
    var grid1 = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns1, rowsPerPage: @rpp);
    if (Request.QueryString[grid1.SortDirectionFieldName].IsEmpty()) {
    grid1.SortDirection = SortDirection.Descending;
    }
    @grid1.GetHtml(    
        mode: WebGridPagerModes. All,
        columns: grid.Columns(
                     grid.Column("ID", "ID"),
                     grid.Column("SSO", "SSO"),
                     grid.Column("Category", "Category"),
                     grid.Column("System", "System"),
                     grid.Column("Subject", "Subject"),
                     grid.Column("Created", "Created"),
                     grid.Column("Assigned_To", "Assigned To"),
                     grid.Column("Case_Status", "Status")
    )
    )
} else .............

这是表格:

<h4>Rows Per Page</h4>
<form action="" method="get">
    <div class="right">
        <select id="rpp1" name="rpp1">
            <option selected="@(Request["rpp1"] == "15")" value="15">15</option>
            <option selected="@(Request["rpp1"] == "25")" value="25">25</option>
            <option selected="@(Request["rpp1"] == "50")" value="50">50</option>
            <option selected="@(Request["rpp1"] == "75")" value="75">75</option>
            <option selected="@(Request["rpp1"] == "100")" value="100">100</option>
        </select>
    </div>
    <input type="submit" name="action" value="Go"/>
  </form>
4

1 回答 1

1

正如错误消息告诉您的那样,错误的数据类型作为参数传递给 WebGrid 构造函数。请求项是字符串,而不是整数。您可以使用 AsInt() 扩展方法来更改它:

var rpp = Request["rpp1"].AsInt();

此外,在代码块内时不需要 @ 符号:

var grid2 = new WebGrid(data, ajaxUpdateContainerId: "grid", defaultSort: "ID", columnNames: columns2, rowsPerPage: rpp);
于 2013-07-09T20:02:31.653 回答