0

What's the effective difference between using a lambda expression and a direct object reference in a strongly typed view? Obviously, lambda expressions are much longer, but they also seem to have some magic I don't yet grasp.

Here is an example... In this case, both item.Title lines print the title of the post. You can imagine why I prefer the shorter one...

@foreach (var item in Model.Posts)
{
   @item.Title
   @Html.DisplayFor(modelItem => item.Title)
}

However, in this case (accessing an object referenced by the object), only the lambda works. I get an "Object reference not set" error with the first line.

@foreach (var item in Model.Posts)
{
   @item.User.UserName
   @Html.DisplayFor(modelItem => item.User.UserName)
}

If anyone could illuminate what's going on here for a novice developer, I'd certainly learn something.

Methodician
  • 2,396
  • 5
  • 30
  • 49

1 Answers1

1

See Remarks section of DisplayFor method documentation. It says:

This method is typically used to display values from the object that is exposed by the Model property. For more information about the differences between this method and the other Display methods, see the DisplayExtensions class overview. This method generates different HTML markup depending on the data type of the property that is being rendered, and according to whether the property is marked with certain attributes. The method renders markup according to the following rules:

  • If the property is typed as a primitive type (integer, string, and so on), the method renders a string that represents the property value.

  • If the property type is Boolean, the method renders an HTML input element for a check box. For example, a Boolean property named Enabled might render markup such as the following: <input class="check-box" disabled="disabled" type="checkbox" checked="checked" />

  • If a property is annotated with a data type attribute, the attribute specifies the markup that is generated for the property. For example, if the property is marked with the EmailAddress attribute, the method generates markup that contains an HTML anchor that is configured with the mailto protocol, as in the following example: <a href='mailto:joe@contoso.com'>joe@contoso.com</a>

  • If the object contains multiple properties, for each property the method generates a string that consists of markup for the property name and markup for the property value.

Not using DisplayFor method on the other hand, does nothing of the above, it simply displays the value as is. (Note: @ Razor syntax implicitly performs HTML encoding, so the previous sentence is not formally 100% correct)

If you would like to understand a little bit better the role of Lambda in a DisplayFor expression, you might find this answer useful.

Community
  • 1
  • 1
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • Thanks @zespri! This answer helps clarify the "what's the difference?" and "why should I use it?" questions while your linked answer helps me better grasp the inner workings of the whole Lambda system in MVC. You're pretty awesome! I'll probably give you the green check unless someone comes along with something better... – Methodician Dec 29 '15 at 19:02