3

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.

1

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.