0

Is there a way to use the [DisplayName("My descriptive Name")] in headers and just the column name when I display data?

(Without having to type each one in?) - (I used MVC scaffolding to create the edit page)

<p>
    @Html.ActionLink("Create New", "CreateTask")
</p>
<table>
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    </tr>
<td>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
</tr>
}
</table>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Danimal111
  • 1,976
  • 25
  • 31
  • What is the distinction you make between 'headers' and 'column name'? to me they are they same.... – solidau Dec 11 '13 at 22:44
  • Possible duplicate of this: http://stackoverflow.com/questions/5250735/how-can-i-use-displayname-data-annotations-for-column-headers-in-webgrid. – ataravati Dec 12 '13 at 01:28

1 Answers1

0

Add properties Display in Model like this

[Display(Name = "Your Custom Name")]
 public string Name { get; set; }

And in Razor View, use

@Html.LabelFor(model=>model.Name)

Hope this help you DAN.

Quan Truong
  • 154
  • 7
  • DAN, are you yahoo like popeprograming..... We've contacted before . Please let me know if you have problem with MVC4 – Quan Truong Dec 12 '13 at 08:16
  • Quan Troung - No I am not popeprogramming on yahoo. And yes, I am doing this already. What I am hoping to do is pull the "Description" in my SQL Server table schema for my input form and use the field name (`Name`) on my gridview. The descriptive [Display(Name...)] is too long for my gridview when displaying the data. but I want to text in my description to allow me to use scaffolding for input screens. Let me know if that makes sense. Thanks! – Danimal111 Jan 09 '14 at 19:34