简体   繁体   中英

Understanding WebGrid in C#

I am trying to get to grips with WebGrid in ac# MVC4 project. The following code gives this error...

Compiler Error Message: CS1502: The best overloaded method match for '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 some invalid arguments

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

The problem is that WebGrid expects your model to be IEnumerable<dynamic> , not IEnumerable<int> . Change your code to the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM