How to write items in view dynamically in MVC


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;}

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

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.




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

Checkout the link below which will display the 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