Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

c# - MVC HTML Helpers and Lambda Expressions

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.

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

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!

So the question really is,

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

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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).

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 :)

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...