As ListOfPersons
is not a declared variable, and is just the parameter name for the expression, the expressions are valid.
To briefly touch on expressions, they are composed of both a parameter set and a body.
(parameter set) => (body)
The parameter set can be empty ()
or include 1 or many parameters (x)
or (x,y)
for example. The body then can use those parameters similar to how a method body would use parameters passed in.
@Html.DisplayFor(ListOfPersons => item.Id)
when used in the scope shown is ignoring the parameter. It doesn't use it, and is similar to something like this
public int Id = 5;
public string DisplayFor(Person ListOfPersons)
{
return Id;
}
So you can see from this aspect that the parameter is not used and the value returned is actually a value from a different scope.
DisplayFor is scoped to use the page's model to bind to. So regardless of the parameter name, what is passed in to the parameter is going to be the model. As such, since the parameter is being completely ignored here, it doesn't particularly matter what it was named and could simply be ()
or _
.
The returned value is then the value from the body, in this case item.Id
and item.Name
. However, as a result of there being no use of the parameter, the html rendered will be incorrect even though the value shown will be what looks to be accurate.
In order to remedy this, the model must be properly accessed or the rendered html will not be bound on post. This is typically done by iterating and using an index reference, as is shown in @Jonespolis' answer.