I just started doing web development using asp.net mvc2. I am trying to find a way to display a collection of data in my opinion. Below is a very simple layout markup to display the collection as an html table.
my question will be what people usually do when building a table from a collection. How to handle a column heading? I have a โDisplayNameโ attribute for all the properties of an object and would like to use them as table column headers.
thanks,
<table> <thead> <tr> <th>???</th> <th>???</th> <th>???</th> <th>???</th> <th>???</th> </tr> </thead> <tbody> <% foreach(var item in Model) { %> <tr> <td><%= Html.Encode(item.MyProp1)%></td> <td><%= Html.Encode(item.MyProp2)%></td> <td><%= Html.Encode(item.MyProp3)%></td> <td><%= Html.Encode(item.MyProp4)%></td> <td><%= Html.Encode(item.MyProp5)%></td> </tr> <% } %> </tbody> </table>
and my class is as follows
public class MyClass { [DisplayName("Dif Prop 1")] [DataMember] public string MyProp1{ get; set; } [DisplayName("Dif Prop 2")] [DataMember] public string MyProp2{ get; set; } [DisplayName("Dif Prop 3")] [DataMember] public string MyProp3{ get; set; } [DisplayName("Dif Prop 4")] [DataMember] public string MyProp4{ get; set; } [DisplayName("Dif Prop 5")] [DataMember] public string MyProp5{ get; set; } }
source share