1

我正在尝试在 ac#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)' 有一些无效参数

@{
    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>
4

1 回答 1

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>

于 2013-08-21T18:59:47.110 回答