Cartesius00
Cartesius00

Reputation: 24384

ASP.NET Razor and table from SqlConnection

I have an existing SqlConnection conn; in some controller (using ASP.NET MVC3 + Razor). Now, I would like to render a simple table depending on some SQL command.

The question is: How to "bind" loaded data in Razor using ViewBag? Is it necessary to iterate row-after-row and produce <tr>....</tr> in Razor?

Upvotes: 0

Views: 699

Answers (2)

Craig
Craig

Reputation: 7076

I'd suggest you to use strongly typed views and pass a populated model to your view and as you mentioned, iterate items of the ViewModel. Binding doesn't really have a place in MVC.

Upvotes: 3

musefan
musefan

Reputation: 48425

There is no binding like this. And a simple for loop means it isnt much code either, example...

<table>
@foreach(var row in Model.MyRows)
{
   <tr>
      @foreach(var col in row.Columns)
      {
         <td>@(col.Value)</td>
      }
   </tr>
}
</table>

hope that gives you an idea anyway, and this way you get a lot more control over the style of your rendered table

Upvotes: 4

Related Questions