在C#中理解WebGrid

时间:2013-08-21 18:40:18

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

我正试图在c#MVC4项目中掌握WebGrid。以下代码给出了此错误...

  

编译器错误消息: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)'has   一些无效的论点

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />

    @{
        List<int> obj1 = new List<int>(){ 1, 2, 3, 4 };
        var obj1_i = (IEnumerable<int>)obj1;       
        var grid = new WebGrid(obj1_i);
    }

</head>
<body>
    <div>
        @grid.GetHtml()
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

问题是WebGrid希望您的模型为IEnumerable<dynamic>,而不是IEnumerable<int>。将您的代码更改为以下内容:

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />

    @{
        List<dynamic> obj1 = new List<dynamic>(){ 1, 2, 3, 4 };     
        var grid = new WebGrid(obj1);
    }

</head>
<body>
    <div>
        @grid.GetHtml()
    </div>
</body>