Collection of asp.net mvc mappings in a table with title

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; } } 
+4
source share
1 answer

you can use

 <%: Html.LabelFor(x => x.MyProp1) %> 

To display the display name MyProp1 in the tag tag and

 <%: Html.DisplayFor(x => x.MyProp1) %> 

To display a value according to it, display a template (usually plain text, unless otherwise indicated)

Using <%:%> will also automatically encode the resulting MvcString, as indicated in <% =%>, which does not encode its contents. However, this autocoding is limited to .NET 4.0 environments and will not work in previous versions.

0
source

Source: https://habr.com/ru/post/1314582/


All Articles