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.

mipe34
  • 5,596
  • 3
  • 26
  • 38
Randel Ramirez
  • 3,671
  • 20
  • 49
  • 63
  • See: http://stackoverflow.com/questions/5848940/mvc-html-helpers-and-lambda-expressions – Faust Jan 07 '13 at 10:48

2 Answers2

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.

Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
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>
Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36