I am new to ASP.NET MVC 3. I am trying to create a basic WebGrid to try and learn how this works. Currently, I have the following code:
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
@{
var grid = new WebGrid();
}
@grid.GetHtml()
</p>
When I run this code, I receive an error that says: “A data source must be bound before this operation can be performed.”. My question is, how do I bind this WebGrid to some client-side JSON. I don’t have a back-end database. I’m just trying to learn about the WebGrid without having to wireup a database.
thanks!
As always in an ASP.NET MVC application you start with a view model:
then a controller:
and finally a corresponding view (
~/Views/Home/Index.cshtml):So as you can see the data source of the grid is actually the view model that the controller populated and passed to the view. The actual data could come from anywhere. It’s the controller’s responsibility though to populate the view model and pass it to the view.
And to learn more about the WebGrid control you may take a look at the following article. And here’s another one.