0

In MVC entity controller there is generated view code(below). Author of the tutorial I am following wanted to add HTML helper which cuts Artist's name which are longer than 25 characters.

Then he uses that helper in that manner: @Truncate(item.Artist.Name, 25).

Why DisplayFor(modelItem => item.Artist.Name) was originally used istead of item.Artist.Name?

@helper Truncate(string input, int length) {
    if (input.Length <= length) {
        @input
    } else {
        @input.Substring(0, length)<text>...</text>
    }
}

  @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Genre.Name)
            </td>
            <td>
                @Truncate(item.Artist.Name, 25)
            </td>
            <td>
                @Truncate(item.Title, 25)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.AlbumId }) |
                @Html.ActionLink("Details", "Details", new { id = item.AlbumId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.AlbumId })
            </td>
        </tr>
    }
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    I'm unclear.. what advantage do you think you're losing? In general, DisplayFor doesn't help you much unless you're using a template, or a complex object, or formatting a date or numeric value. DisplayName is only used with LabelFor, or as I said when rendering a complex property. There's no advantage to DisplayName if you're just using DisplayFor on a simple property that doesn't need formatting. – Erik Funkenbusch Jun 28 '14 at 19:11
  • @ErikFunkenbusch I confused `DisplayFor` with `DisplayNameFor` about which I asked before http://stackoverflow.com/questions/24436334/why-in-generated-databasemanagercontroller-table-header-is-generated-using-lambd/24437316#24437316. So I will update question: Why `DisplayFor(modelItem => item.Artist.Name)` is used istead of `item.Artist.Name`? – Yoda Jun 28 '14 at 19:14
  • That doesn't change my comment. DisplayNameFor is only used to create the label or title for an item, not for formatting the object itself. There is no conflict with using @Truncate() as specified and using DisplayNameFor, since they will be called for different purposes – Erik Funkenbusch Jun 28 '14 at 19:16
  • DisplayNameFor returns "Name" above, DisplayFor returns "The Cranberries" or whatever the artists name is. Do you see how they are entirely unrelated? And why you can exchange DisplayFor with @Truncate() without affecting DisplayNameFor? – Erik Funkenbusch Jun 28 '14 at 19:19
  • @ErikFunkenbusch Yes I see how completely they are related I said in comment that I confused them that is why I stated wrong question in the subject and the question I would like to ask is:Why `DisplayFor(modelItem => item.Artist.Name)` was originally used istead of simply `item.Artist.Name`? – Yoda Jun 28 '14 at 19:22
  • Your question has been asked many times before – Erik Funkenbusch Jun 28 '14 at 19:36
  • @ErikFunkenbusch I ask what is the DIFFERENCE between DisplayFor and excplicitly accessing a property and this topic is not discussed in the "duplicate" topic. – Yoda Jun 28 '14 at 19:38

0 Answers0