MVC HTML Helpers and Lambda Expressions

2019-01-08 23:04发布

问题:

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!

回答1:

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



回答2:

You do have the option of using an alternative, similar to what you have above. In this case it would actually be @Html.Display("name") without the 'For' portion, and with the property name of the model passed in as a string. This is an OK alternative. If your property name changes though this will break at runtime.

I like the lambdas because I'm a refactoring junkie who constantly renames things. By turning on view compilation I can then lean on the compiler to help me find the places I need to update my views.