Displaying a Table of Database Data

Please install Silverlight or click download to watch video locally.

Need to display a set of database records in an ASP.NET MVC view? Learn two methods of displaying records in an HTML table. You learn how to perform all of the formatting inline. You also learn how to create a template with a ASP.NET MVC user control.

Presented by Stephen Walther

Duration: 15 minutes, 7 seconds

Date: 20 August 2008

Watch    Video   |   Download    Video

Video downloads: WMV  |  Zune  |  iPod  |  PSP  |  MPEG-4  |  3GP

Audio downloads: AAC  |  WMA  |  MPEG-4  |  MPEG-3  |  MPEG-2

Comments : 24

Leave a Comment

cv_vikram : On August 27, 2008 7:01 AM said:

thanks....

sn33zingpanda : On September 15, 2008 10:47 PM said:

Trying to follow along with your vb code in C#.  When I run the app for the first time, I get

Line 13: <% foreach (Call c in ViewData.Model)%>

CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

I've tried to remove my dbml and re-create it following your pattern.  I have a Calls table that I have dropped onto my Call model.

Not sure if there's a difference between C# and VB that's biting me here, or if I'm simply missing a step.

Good video overall... high quality video and audio, good presentation.

swalther : On September 16, 2008 2:29 PM said:

@sn33zingpanda -- Yes, this is a difference between C# and VB.NET. In C#, you must explicitly cast ViewData.Model to an IEnumerable in your foreach loop:

<% foreach (var c in (IEnumerable)ViewData.Model) { %>

The other option is to use a strongly typed view. When you create a strongly-typed view, you specify the type of the ViewData.Model property in your code-behind class.

amanprogrammer : On September 16, 2008 5:24 PM said:

Stephen You rock.

sboys : On October 22, 2008 7:00 AM said:

I am trying to add a Mvc View user control as shown in the video but the code in the Index.aspx page is failing on the RenderUserControl line.

<% foreach (Project p in (IEnumerable)ViewData.Model)

  { %>

       <%= Html.RenderUserControl (ViewData.Model, "~/Views/Home/ProjectTemplate.ascx", p); %>

<% } %>

The error message is 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderUserControl' and no extension method 'RenderUserControl' acceptin a first argument of type 'System.Web.Mvc.HtmlHelper' could be found.

I have the following in the Index.aspx.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MvcCorporate.Models;

namespace MvcCorporate.Views.Home

{

   public partial class Index : ViewPage

   {

   }

}

How do I get access to the RenderUserControl?

fchalip : On October 22, 2008 8:13 PM said:

How about displaying 2 tables in the View.

Let say we want to display 2 different LinkToSql Queries (Top 5 New Releases and Movies directed by  Steven Spielberg)

How we do that if return View on the Controller only accepts one Model object?

Francisco

mstum : On October 22, 2008 8:17 PM said:

Thank you! I'm even going to overlook that "Star Wars II" mistake you did in the movie table :P Good work on the videos, they rock!

anthonywjones66 : On November 16, 2008 3:53 PM said:

Preview 5 is now using Html.RenderPartial instead of RenderUserControl

worldlifesite : On December 21, 2008 1:31 PM said:

Another very great series.  This really helps those new to MVC, makes it quick to pick up.

DaseinMind : On December 23, 2008 3:27 PM said:

Microsoft definitely ought to provide video series of this quality together for every major technology.  However, is anyone else hearing creepy bubbles and noises in the background (11:24)?

DotNetNoobiee : On January 07, 2009 1:32 PM said:

Hi everyone, hope i can get some help on this, and I have a gut feeling that this will be considered as an extremely stupid question, but i'm also a REAL noob when it comes to .Net.

I've looked at a lot of the tutorial videos, which are amazing btw, and i can follow along quite nicely.

I tried a simple Mvc application using Linq2SQL and C#.

I'm getting an "InvalidCastException was unhandled by user code" error when i try to display my client table the same as in the video (except i'm doing it in C#).

this is my code.

AdminController:

       public ActionResult ClientList()

       {

           myDataContext dataContext = new myDataContext();

           var client = from c in dataContext.Clients   select c;

 return View(client);

}

clientList.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="ClientList.aspx.cs" Inherits="cbia.Views.Admin.ClientList" %>

<%@ Import Namespace="MvcApplication1.Models" %>

   

   <tr>

       <td>test data</td>

   </tr>

       <%            

           foreach (Client c in (IEnumerable)ViewData.Model)

           {

       %>

       <tr>

           <td>

               <%= c.name%>

           </td>

           <td>

               <%= c.address%>

           </td>

           <td>

               <%= c.isActive%>

           </td>

       </tr>

       <% } %>

   </table>

</asp:Content>

I would appreciate any guidance in this matter!

DotNetNoobiee : On February 02, 2009 3:41 PM said:

Hi peeps,

I managed to fix the problem by erasing all the tables from my .dbml file and re-import the tables, i suppose the project was corrupt or something, hence why i was getting the exception.

.netDeveloper22 : On February 04, 2009 10:04 AM said:

Thanks Stephen

marafacs : On March 20, 2009 3:34 AM said:

Thanks stephen... excellent video

pradipjalu : On April 11, 2009 6:51 AM said:

Thanks Stephen for providing nice video tutorial on table data format in ASP.Net MVC application.

vinothkumarsi : On April 18, 2009 7:26 AM said:

Thanks Stephen

kwilson1997 : On May 05, 2009 4:10 PM said:

I had a rough time following along using C#.NET 2008. I finally got it to work.

~/Views/Home/Index.aspx :

=========================

<%@ Import Namespace="MvcSample.Models"%>

...

<%

foreach (Movie m in (IEnumerable) ViewData.Model) {

Html.RenderPartial("~/Views/Home/MovieTemplate.ascx", m);

}

%>

NOTE: "RenderUserControl()" does not exist. I'm guessing it was replaced with "Html.RenderPartial()" which is what I used above.

~/Views/Home/MovieTemplate.ascx:

================================

<%@ Import Namespace="MvcSample.Models"%>

<tr>

<td><%= (ViewData.Model as Movie).Id %></td>

<td><%= Server.HtmlEncode((ViewData.Model as Movie).Title)%></td>

<td><%= (ViewData.Model as Movie).DateReleased.ToShortDateString()%></td>

</tr>

NOTE: There is no code behind file for this control. None was ever created. I can't see any way to strongly type the current record except to use "(ViewData.Model as Movie)" as I did in my code above. Is there a better way to do this?

stallings_st@hotmail.com : On May 18, 2009 8:21 PM said:

I have a hard time tried to figure out.... why no code behind file when i created mvc user control. Thank you kwilson1997.

Talley123 : On May 20, 2009 5:12 AM said:

Great video for MVC novice like me.Thanks

weitzhandler : On June 18, 2009 1:40 AM said:

thanks!

currentspulledmeunder : On July 03, 2009 1:20 PM said:

I wish this was in C#, I'm having trouble following. My dataContext doesn't have a GetTable command, just the fields of my table. I'm not sure what I did wrong.

francke peixoto : On August 24, 2009 10:42 AM said:

very good! ;-)

zeyzone : On October 09, 2009 5:18 PM said:

Good work

shadyrudy : On October 15, 2009 9:58 PM said:

Wish there was a C# version. :/

Leave a Comment

You must be logged in to leave a comment. Click here to log in.