0

I am new to C# and I'm not sure I understand this piece of code:

@Html.DisplayNameFor(model => model.isAlive)

From what I searched online, the lambda operator would be shorthand for a function such as parameter => expression.

  • Is this what is being used here?
  • If so, is the function being called automatically since it has no name?
  • Is a model object being implicitly passed in the 'model' parameter?

This is the full code:

@model IEnumerable<FirstMVC.Models.PersonModel> //retrieving the passed list (people) object, it becomes IEnumerable

@{
    ViewBag.Title = "ListPeople";
}

<h2>ListPeople</h2>

<p> <!--Here, only 2 params because it's assuming using PeopleController-->
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th><!--Displays column NAMES, notice DisplayNameFor-->
            @Html.DisplayNameFor(model => model.FirstName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.LastName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.isAlive)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td> <!--Displays row VALUES-->
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Age)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.isAlive)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>
diagoot
  • 39
  • 1
  • 7
  • Does this answer your question? [Why would you use Expression> rather than Func?](https://stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct) – madreflection Aug 31 '20 at 16:12
  • A little demo to show that `Expression` can be used to view the content of the lambda: https://dotnetfiddle.net/fUCMn9. In your case the framework uses the structure generated by the expression to extract the property name so they can then use that to generate a display for that property. –  Aug 31 '20 at 17:08

1 Answers1

2

Is this what is being used here?

Yes, it is defining an expression to be used

If so, is the function being called automatically since it has no name?

No. Just the expression is being passed, not the result of that expression. In this case, the expression itself is never called - it is just analyzed to determine what member is being referenced (the isAlive property in this case) so that the input can be created, named, and bound to the member on callback.

Is a model object being implicitly passed in the 'model' parameter?

A model is not being "passed", but the Razor compiler will use the type defined in the @model {typename} directive to determine the type of the model parameter.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thank you that was clear. Could you just clarify where is the ```<%model...%>``` directive you speak of which defines the type used? – diagoot Aug 31 '20 at 20:09
  • It should be at the top of the Razor page. And I had the syntax wrong, it should be `@model MyApp.MyModel` – D Stanley Aug 31 '20 at 20:42