Creating a simple datagrid in ASP.NET MVC

1) In your controller, query all of the records from the database using LINQ to SQL and pass the data to the view:

AppsDataContext appsdata = new AppsDataContext();
var all_apps = from a in appsdata.apps select a;
ViewData["apps"] = all_apps;
return View();

2) In your view, iterate through the apps, make sure to cast the ViewData value as System.Linq.IQueryable, put the data into a html table

<table border=”1″>
<tr><td><b>First Name</b></td><td><b>Last Name</b></td></tr>

<%       foreach (var a in (System.Linq.IQueryable)ViewData["apps"]) %>
<% { %>
<tr>
<td><%= ((Models.app)a).firstname1 %></td> <td><%= ((Models.app)a).lastname2 %> </td>
</tr>
<% } %>

</table>

3) style your html table as you like and add links where needed to get whatever traditional datagrid functionality you desire.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.