How to write items in view dynamically in MVC

Hi,

Following is a working code for displaying records in view

@using MvcWcf.ServiceReference1
@model IEnumerable<WcfService.MyAddress> 


<style type="text/css">
table,tr,td{border:1px solid black;border-collapse:collapse;}
</style>

<table>
<tr>
<td>@Html.DisplayNameFor(model => model.Address1)</td><td>@Html.DisplayNameFor(model => model.City)</td>
</tr>
@foreach (var item in Model)
{
    <tr><td>@Html.DisplayFor(modelItem => item.Address1)</td><td>@Html.DisplayFor(modelItem => item.City)</td>
</tr>
}
</table>

in the above code i wrote table header name and fieldnames manually.because i know i the field names.

how to write the table header name and field names automatically.

regards

Baiju

Hi,

You wanted to display the header names from the Model? i.e.. the properties in the Model. If that is the case you can use the
Reflection 

Checkout the link below which will display the property names. 

http://www.csharp-examples.net/reflection-property-names/

should it write reflection in controller

No. Write within the html.

I tried with your link.doesn’t work

do you have any sample code

can i get any sample code

Leave a Reply