简体   繁体   English

linq / lambda中的Asp.net mvc html.DisplayFor语法

[英]Asp.net mvc html.DisplayFor syntax in linq/lambda

I'm currently studying asp.net mvc and I just started, I decided to move away from web forms to mvc. 我正在学习asp.net mvc,我刚开始,我决定从网络表格转移到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. 我理解linq和lambdas的基础知识,但我想知道或得到关于这个特定语法的一个很好的解释。

@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 我想知道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() . 我对此的了解是, modelItem获取值item.Genre.Name ,然后传递方法Html.DisplayFor()

I'm also curious about how do I write the same code without using lambda. 我也很好奇如何在不使用lambda的情况下编写相同的代码。

Correct me if I'm wrong I would just like to know the meaning of the code and how it is read. 纠正我,如果我错了,我只想知道代码的含义以及如何阅读。

Read this: Why All The Lambdas? 阅读本文: 为什么所有的Lambdas? : Good article explaining the use of Lambdas. :解释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. lambda表达式(Expression类型)允许视图作者使用强类型代码,同时为HTML帮助程序提供完成工作所需的所有数据。

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>

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

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