I Want to Understand the Lambda Expression in @Html.Displayfor(Modelitem => Item.Firstname)

I want to understand the lambda expression in @Html.DisplayFor(modelItem = item.FirstName)

A lambda expression is a way to write an anonymous function, i.e. a function without a name. What you have on the left side of the "arrow" are the function parameters, and what you have on the right side are the function body. Thus, (x => x.Name) logically translates to something like string Function(Data x) { return x.Name } the types string and Data will obviously vary and be derived from the context.

The absence of the left-side parameter translates into a function without parameters, so that this (() => someVariable) logically translates to this: string Function() { return someVariable; }

At this point you might start wondering, where someVariable comes from, it's not a function parameter and it is not defined in the function. You would be correct, a function like this would never compile. However the lambda function like this is perfectly fine, as it allows outer-scope variables be lifted and used this way. (Internally a wrapper class is created where the variables that are used in the lambda expression become fields.)

Now let's see what model => item.FirstName means. Logically it would translate to string Function(Model model) { return item.FirstName; }. Basically this is a function with a parameter, but this parameter is not used.

And now, the last bit of the information. Although lambda expressions represent functions in the end, sometimes they are created not with the purpose of actually being executed (although potentially they can). A lambda expression can be represented in the form of an expression tree. This means that instead of executing it it can be parsed.

In this particular case the MVC engine does not run the function that the lambda expression represents. Instead the expression is parsed so that MVC engine knows what html to emit for this particular line. If your data item does not come from your model object directly, the left part of the lambda expression does not matter.

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

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>

Confused about model meaning(s) in ASP.NET views

This @model EFDBfirst.Models.Student declares that the model that is used in this view is the Student.

Inside you view you can refer to your model, using the Model. So if you want to iterate though the Enrollments of the student object you have passed to this View, you just have to do this:

@foreach(var enrollment in Model.Enrollments)
{

}

Regarding this, @Html.DisplayNameFor(model => model.LastName) this is an HTML helper, that will create for you a label in this case for the property of you model that is called LastName.

Regarding html helpers, please have a look here. Furthermore, you could pass to this helper this line:

@Html.DisplayNameFor(m => m.LastName)

or this line:

@Html.DisplayNameFor(x => x.LastName)

It doesn't matter that you made use of model. This is just a reference to the model you have passed to the View.

Last but not least, this @Html.ActionLink("Edit", "Edit", new { id = Model.StudentID }) is also an html helper that creates an html link.



Related Topics



Leave a reply



Submit