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

前端 未结 4 542
鱼传尺愫
鱼传尺愫 2020-11-27 14:46

I’m fairly new at C# and MVC and have used lambdas on certain occasions, such as for anonymous methods and on LINQ.

Usually I see lambda expressions that look someth

4条回答
  •  佛祖请我去吃肉
    2020-11-27 15:05

    i think it's about the foreach loop. example:

    @foreach(var item in model) 
    { 
      
         @html.displayfor(model => item.firstName) 
          
    }
    

    var item needs to be used because each item in the sequence is an anonymous type. model => item.firstName means (input parameter) => expression. you can't use the input parameter because we store the current "item" in item.

提交回复
热议问题