本文介绍了基本MVC:这是什么" modelItem =>"做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个经常由Visual Studio在MVC类型的应用程序产生code的样本行:

Here is a sample line of code that is often generated by Visual Studio in an MVC type of application:

@Html.DisplayFor(modelItem => item.LastName)


  • 我了解剃刀工程(即 @

  • 我理解HTML是静态辅助功能,如 DisplayFor()
  • 对象
  • 我理解 item.LastName ,因为这松散的重新presents从数据/模型行和列

    • I understand how razor works (the @)
    • I understand Html is an object with static helper functions, like DisplayFor()
    • I understand item.LastName as this loosely represents a row and column from data/model
    • ...但到底是什么 modelItem => ?早在我的一天, => 曾经是被评价为一个布尔值的运营商。这是什么魔法?

      ...but what the heck is modelItem =>? Back in my day, => used to be an operator that was evaluated to a Boolean value. What is this sorcery?

      推荐答案

      你在做什么有一个路过的lambda前pression。这些都是基本相同的代表,函数指针在C或函数在Javascript中。 Youare基本上是告诉的Html DisplayFor使用此函数来获取显示项目。您的示例实际上应该可能是:

      What you are doing there is passing a lambda expression. These are essentially the same as delegates, function pointers in C or functions in Javascript. Youare basically telling Html DisplayFor "use this function to get the display item". Your example should actually probably be:

      @Html.DisplayFor(modelItem => modelItem.LastName)
      

      否则,你正试图从外部范围关闭项。如果这是你真正想要做什么,然后modelItem基本上无所事事...

      Otherwise, you are trying to close "item" from an outer scope. If this is what you are really trying to do, then modelItem is doing essentially nothing...

      看http://msdn.microsoft.com/en-us/library/bb397687.aspx

      这篇关于基本MVC:这是什么" modelItem =>"做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 01:53