views:

412

answers:

4

I am looking into binding some data. What method of output would be better. I usually use Gridviews, but I am unsure if the table would be better now.

A: 

That really depends on what you are outputting. I only use tables / gridview for tabular data.

For other stuff i use Repeater controller or FormView.

Steven
+3  A: 

If all you're looking to do is present(output) a table of data, no need for a gridview at all (besides, in MVC, you most likely wouldn't want to anyway).

Simply loop through your model collection and create a new table row for each item:

<table>
<% foreach (var item in Model) { %>
<tr><td>FieldName</td><td><%= Html.Encode(item.Field) %></tr>
<% } %>
</table>

you can format up your table however you like by applying the appropriate css class to it.

EDIT: a better example to show multiple fields, also showing how to display no data.

                 <% if (Model.Count() > 0)
                { %>
                    <table width="35%">
                 <thead><tr><th>Species</th><th>Length</th></tr></thead>
                  <%  foreach (var item in Model)
                    { %>
                     <tr>
                         <td><%= item.FishSpecies%></td>
                         <td align="center"><%=item.Length%></td>
                     </tr>

                 <% } %>
                    </table>
                <% }
                else
                { %>
                No fish collected.
                <%} %>
Jamie M
How is that going to work if you have more than 1 field or more than 1 row (dependent on your orientation, data that is)?
Lazarus
i was thinking about my example on the way to lunch, and how limited it was. i edited above :)
Jamie M
+4  A: 

You should really steer clear of ASP.NET Web Forms controls when using the MVC framework. They don't play nicely together 100% of the time.

The HTML helpers/ standard HTML elements are the replacement. The idea being that you have more control over what you're doing that with Web Forms.

There are some nice grids available:

  1. jqGrid (example of use with MVC here)
  2. And the awesome MVCContrib project.
Kieron
A: 

While you can partially use ASP.NET server controls in MVC applications (e.g. a menu will render quite well), you should expect that they won't expose the expected or rich behavior you are used to see in ASP.NET. There's a number of reasons for that:

  • They typically rely on ViewState (and ControllerState)
  • You are required to put them inside a form tag with runat="server". Most controls check that this is the case indeed
  • Related to this, they rely heavily on the postback paradigm, which is not used in ASP.NET MVC

As a typical example of behavior that you won't find back, there is paging and sorting ...

When you delve into it, you soon find out that the helpers available in the MVC framework allow you to generate tabular data quite efficiently ....

sergevm