Is HTML table better than Gridview in ASP.NET MVC?

I am looking into some data binding. Which withdrawal method will be better. I usually use Gridviews, but I'm not sure if the table will be better now.

+2


source to share


4 answers


When using the MVC framework, you should avoid ASP.NET Web Forms controls. They don't play well together 100% of the time.

HTML helpers / standard HTML elements are a replacement. The idea is that you have more control over what you do with Web Forms.



Some available meshes are available:

+4


source


If all you want to do is render (output) a table of data, there is no need for a gridview at all (besides, in MVC you probably don't want to anyway).

Just go through the collection of models 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 the table however you like by applying the appropriate css class to it.

EDIT: Better example for displaying multiple fields, also showing how to display 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.
                <%} %>

      

+4


source


It really depends on what you output. I am using tables / gridview for tabular data.

For other things, I use a relay controller or FormView.

0


source


While you can partially use ASP.NET server elements in MVC applications (for example, menus will render well enough), you should expect them not to show the expected or rich behavior that you use to view in ASP.NET. There are a number of reasons for this:

  • They usually rely on ViewState (and ControllerState)
  • You must put them in the form tag with runat = "server". Most of the controls verify that this is the case.
  • Because of 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, there is paging and sorting ...

As you dig deeper into this, you will soon find out that the helpers available in the MVC framework allow you to efficiently create tabular data ....

0


source







All Articles