简体   繁体   中英

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?

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

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.

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