简体   繁体   English

@ Html.DisplayNameFor()中的Lambda表达式

[英]Lambda expression in @Html.DisplayNameFor()

Let's say I am passing a List of Person as the model to a view page: 假设我正在将“ Person List ”作为模型传递给视图页面:

@model IEnumerable<Foo.Models.Person>

@foreach (var item in Model)
{
    <p>
        @Html.DisplayFor(ListOfPersons => item.Id) : @Html.DisplayFor(ListOfPersons =>item.Name)
    </p>    
}

I just don't get how this Lambda expression works. 我只是不知道这个Lambda表达式如何工作。 Why we are passing a IEnumerable and get a single value of on its objects? 为什么我们要传递IEnumerable并在其对象上获得单个值?

As ListOfPersons is not a declared variable, and is just the parameter name for the expression, the expressions are valid. 由于ListOfPersons不是声明的变量,而只是表达式的参数名称,因此表达式有效。

To briefly touch on expressions, they are composed of both a parameter set and a body. 为了简要介绍表达式,它们由参数集和主体组成。

(parameter set) => (body)

The parameter set can be empty () or include 1 or many parameters (x) or (x,y) for example. 参数集可以为空()也可以包含1个或多个参数(x)(x,y) The body then can use those parameters similar to how a method body would use parameters passed in. 然后,主体可以使用那些参数,类似于方法主体将使用传入的参数的方式。

@Html.DisplayFor(ListOfPersons => item.Id) when used in the scope shown is ignoring the parameter. 在显示的范围中使用@Html.DisplayFor(ListOfPersons => item.Id)时,将忽略该参数。 It doesn't use it, and is similar to something like this 它不使用它,并且类似于这样

public int Id = 5;
public string DisplayFor(Person ListOfPersons)
{
    return Id;
}

So you can see from this aspect that the parameter is not used and the value returned is actually a value from a different scope. 因此,从这一方面可以看出,未使用该参数,并且返回的值实际上是来自其他范围的值。

DisplayFor is scoped to use the page's model to bind to. DisplayFor的作用域是使用页面模型进行绑定。 So regardless of the parameter name, what is passed in to the parameter is going to be the model. 因此,无论参数名称是什么,传递给参数的都是模型。 As such, since the parameter is being completely ignored here, it doesn't particularly matter what it was named and could simply be () or _ . 这样,由于这里完全忽略了该参数,因此它的名称并不重要,可以简单地是()_

The returned value is then the value from the body, in this case item.Id and item.Name . 然后,返回值是来自正文的值,在本例中为item.Iditem.Name However, as a result of there being no use of the parameter, the html rendered will be incorrect even though the value shown will be what looks to be accurate. 但是,由于没有使用该参数,因此即使显示的值看起来是准确的,呈现的html也将是不正确的。

In order to remedy this, the model must be properly accessed or the rendered html will not be bound on post. 为了解决这个问题,必须正确访问模型,否则渲染的html不会在发布时绑定。 This is typically done by iterating and using an index reference, as is shown in @Jonespolis' answer . 这通常是通过迭代和使用索引引用来完成的,如@Jonespolis的answer所示。

use a for loop in razor, so you maintain a direct reference to your model: 在剃须刀中使用for循环,因此您可以直接引用模型:

@for(int i =0; i < Model.Count(); i++)
{
   <p>
    @Html.DisplayFor(model => model[i].Id) : @Html.DisplayFor(model => model[i].Name)
   </p>    
}
 @Html.DisplayFor(ListOfPersons => item.Id) : @Html.DisplayFor(ListOfPersons =>item.Name) 

The view engine will examine the expression and determine that you want "display" controls for the Id and Name properties. 视图引擎将检查表达式,并确定您要“显示” IdName属性的控件。 It ignores the "input" variable name ( ListOfPersons * in this case) and the item variable and just parses the expression on the right. 它忽略“输入”变量名称(在这种情况下为ListOfPersons *)和item变量,仅解析右侧的表达式。 You could just as well have done this: 您也可以这样做:

@Html.DisplayFor(_ => item.Id) : @Html.DisplayFor(_ => item.Name)

*Note that ListOfPersons in your lambda does NOT reference a local variable - it just creates a new "variable" that you could reference in your lambda. *请注意,您的lambda中的ListOfPersons不会引用局部变量-它只是创建一个可以在lambda中引用的 “变量”。 The fact that you seem to have a property or local variable named ListOfPersons is irrelevant. 您似乎拥有一个名为ListOfPersons的属性或局部变量的ListOfPersons是无关紧要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM