Generally, that is called a lambda expression.In your scenario, you are telling the DisplayNameFor
method that "take my model, and create a display element for this property.".You can't use model.City
, because it just returns the value of the property.The method needs more than that in order to create a display element for your property.For example, it needs to know it's type and also it's attributes (like DisplayName
attribute) and then it creates a display element for your element(it should be label I guess) .
DisplayName
method is doing that using Expression Trees.The method takes an Expression<Func<TModel, TValue>>
and uses it to get the name, value and the metadata information (attributes) about your property.
If you want to use model.City
you can still use it, but then you won't need the functionality that DisplayNameFor
provides.If you just need to display value of the property you can always do it like this:
<label> @model.City </label>
I understand this is Linq query,
Btw, this is incorrect, that is not a LINQ
query.That is just an extension method.