MVC HTML助手和Lambda前pressions [英] MVC HTML Helpers and Lambda Expressions

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

问题描述

据我所知大部分LAMBDA查询,但是当我努力学习MVC,我看到默认的脚手架模板,他们使用LAMBDA前pressions这么多的组件。

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帮助。在code推移 @ Html.DisplayFor(型号=> model.name)

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

我希望没有人认为这是一个愚蠢的问题,它只是虽然我(我想)知道大部分LAMBDA前pressions,他们没有流像普通code和我得想想挺难理解什么是真正发生!

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)是否有使用lambda查询这些HTML助手?,我很想念他们任何好处。

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前pressions实际上是。

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

在.NET中,使用这种方式前LAMBDA pressions是所谓的防爆pression树。从 MSDN

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

实施例pression树木重新present $ C $下在树状数据结构,其中每个节点是一个前pression,例如,一个方法调用或二进制操作如x <年。

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 =方式&gt;的模式。名称)它传递的数据结构,上面写着:我呼吁XXXX数据结构的名称属性(其中xxxx是数据结构,重的类型,这种方法presents您的浏览模型)。

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 然后查看这些数据并看到属性名名称是好名称,它着眼于该属性的所有属性,以找出是否有连接到它的任何数据的注释,然后着眼于价值,以确定如何重新present该值的显示。这是一个有点复杂,直到你得到你的头包裹着它,但一旦你看看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前pressions的好处是你编译时你的财产清查。例如,如果您重命名 ViewModel.Name ViewModel.ClientName ,所有的 HTML。 DisplayFor(X =&GT; model.Name)将不能编译,从而确保您对其进行更改。如果你不使用拉姆达前pressions,所有的 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,道理是一样的我当然pression树的描述。如果不使用lambda表达式,你只是传递 Model.Name 的值与没有对物业本身的信息,所以它不知道 Model.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.

防爆pression树的另一个好写了,可以发现<一个href=\"http://rapidapplicationdevelopment.blogspot.com/2008/03/ex$p$pssion-trees-why-linq-to-sql-is.html\"相对=nofollow>此处。了解前pression树木在.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前pressions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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