Randel Ramirez
Randel Ramirez

Reputation: 3761

Asp.net mvc html.DisplayFor syntax in linq/lambda

I'm currently studying asp.net mvc and I just started, I decided to move away from web forms to mvc.

I understand the basics of linq and lambdas but I would just like to know or get a good explanation about this particular syntax.

@model IEnumerable<CodeplexMvcMusicStore.Models.Album>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Genre.Name)
        </td>

I would just like to know what is the meaning of modelItem => item.Genre.Name

My knowledge on this is that modelItem gets the value item.Genre.Name and then it is passed method Html.DisplayFor().

I'm also curious about how do I write the same code without using lambda.

Correct me if I'm wrong I would just like to know the meaning of the code and how it is read.

Upvotes: 3

Views: 2161

Answers (2)

Victor Leontyev
Victor Leontyev

Reputation: 8736

You can write

    @model IEnumerable<CodeplexMvcMusicStore.Models.Album>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.Raw(item.Genre.Name)
        </td>

Or

@model IEnumerable<CodeplexMvcMusicStore.Models.Album>

@foreach (var item in Model) {
    <tr>
        <td>
            @item.Genre.Name
        </td>

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Read this: Why All The Lambdas? : Good article explaining the use of Lambdas.

The lambda expressions (of type Expression) allow a view author to use strongly typed code, while giving an HTML helper all the data it needs to do the job.

Upvotes: 5

Related Questions