MVC HTML 助手和 Lambda 表达式 [英] MVC HTML Helpers and Lambda Expressions

查看:26
本文介绍了MVC HTML 助手和 Lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大部分了解 Lambda 查询,但是当我尝试学习 MVC 时,我看到默认的 Scaffolding 模板,它们使用 Lambda 表达式来处理如此多的组件.

I understand Lambda queries for the most part, but when I am trying to learn MVC, and I see the default Scaffolding templates, they use Lambda expressions for so many components.

一个例子是 DisplayFor HTML Helper.代码去 @Html.DisplayFor(model => model.name)

One for example is the DisplayFor HTML Helper. The code goes @Html.DisplayFor(model => model.name)

我希望没有人认为这是一个愚蠢的问题,只是虽然我(认为我)大部分理解 Lambda 表达式,但它们不像常规代码那样流动",我必须仔细考虑很难理解实际发生了什么!

I hope no one thinks this is a stupid question, it is just that whilst I (think I) understand Lambda expressions for the most part, they don't "flow" like regular code and I have to think about it quite hard to understand what is actually happening!

所以问题是,

1) 对这些 HTML 帮助程序使用 Lambda 查询有什么好处吗?

1) is there any benefit that I am missing to them using Lambda queries for these HTML Helpers?

2) 据我所知,DisplayFor 只会连接到一个项目 - 那么,为什么不只是 @Html.DisplayFor(model.name) 或类似的?

2) As far as I can tell, the DisplayFor will only ever be hooked up to one item - so, why isn't this just @Html.DisplayFor(model.name) or similar?

请提供任何其他可以使 MVC 新手变得更好的信息!

And please give any other information that can make a MVC newbie better!

推荐答案

在我回答您的 2 个要点之前,我认为您需要了解 lambda 表达式实际上是什么.

Before I answer your 2 bullet points, I think you need to understand what lambda expressions actually are.

在.Net中,以这种方式使用的Lambda表达式就是所谓的表达式树.来自 MSDN:

In .Net, Lambda expressions used in this way are what is called Expression Trees. From MSDN:

表达式树表示树状数据结构中的代码,其中每个节点都是一个表达式,例如一个方法调用或一个二元运算如 x <;y.

Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

这些本质上是描述传入内容的数据结构,而不是传入数据的值.这意味着当您调用 Html.DisplayFor(x => model.Name) 它传入一个数据结构,上面写着我正在为 xxxx 数据结构的 Name 属性调用此方法(其中 xxxx 是表示您的视图模型的数据结构类型).

These are essentially data structures that describe what is being passed in rather than the values of the data being passed in. What this means is that when you call Html.DisplayFor(x => model.Name) it is passing in a data structure that says "I am calling this method for the Name property of the xxxx data structure (where xxxx is the type of data structure that represents your View Model).

DisplayFor 然后查看此数据,发现属性名称 Name 是好的 Name,它查看该属性的所有属性以找出是否有任何附加到它的数据注释,然后查看该值以确定如何表示该值的显示.它有点复杂,直到您完全了解它为止,但是一旦您查看 MSDN 页面并考虑它,您就会大吃一惊!像我一样的那一刻,它会突然变得有意义:)

The DisplayFor then looks at this data and sees that the property name Name is well Name, it looks at all attributes for the property to find out if you have any data annotations attached to it, and then looks at the value to determine how to represent the display for the value. It's a bit complicated until you get your head wrapped around it, but once you look at the MSDN page and think about it you will have an aha! moment like I did, and it will just suddenly make sense :)

至于您的问题 #1,使用 lambda 表达式的优点是您可以在编译时检查您的属性.例如,如果您将 ViewModel.Name 重命名为 ViewModel.ClientName,则您所有的 Html.DisplayFor(x => model.Name) 都赢了不编译,从而确保您更改它们.如果您不使用 lambda 表达式,则您所有的 Html.Display() 调用都将起作用,但是您将通过模型绑定获得隐藏的错误,这些错误不会立即被发现是什么错误.

As to your question #1, the advantage of using lambda expressions is you get compile time checking of your properties. For example, if you rename ViewModel.Name to ViewModel.ClientName, all your Html.DisplayFor(x => model.Name) won't compile, thus making sure you change them. If you don't use lambda expressions, all your Html.Display() calls will work, but you will get hidden bugs with model binding that will not be immediately obvious of what's wrong.

回答#2,原因与我对表达式树的描述相同.不使用 lambdas,你只是传入 Model.Name 的值,没有关于属性本身的信息,所以它不知道 Model.Name 属性的名称是Name,它看到的只是字符串值.

To answer #2, the reason is the same as my description of expression trees. Without using lambdas, you are just passing in the value of Model.Name with no information about the property itself, so it doesn't know the name of Model.Name property is Name, all it sees is the string value.

可以找到关于表达式树的另一篇很好的文章此处.理解表达式树在 .Net 中发挥了很大的作用 :)

Another good write-up of Expression Trees can be found here. Understanding expression trees opens up a whole lot of power in .Net :)

这篇关于MVC HTML 助手和 Lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆