2

In the following code of the example application of VS2015:

@model LoginViewModel

<p>                               
 @Html.DisplayNameFor(m => m.Name)                                       
</p>

I want to know how the compiler knows that m refers to the LoginViewModel if I never said that m is an alias for LoginViewModel.

And I would like help understanding the lambda expression of DisplayNameFor. Why it requires a lambda if we could just pass the string m.Name? How is it used here?

ekad
  • 14,436
  • 26
  • 44
  • 46
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
  • you can [follow the instructions in this answer](http://stackoverflow.com/questions/383192/compile-views-in-asp-net-mvc) to pre-compile your views, and see what the razor engine is doing. – Jonesopolis Aug 24 '16 at 15:43

3 Answers3

4

The given Razor template will be compiled, so the Razor view compiler can do some magic here.

The compiler knows the type of your model because of the @model directive (without the @model directive the compiler falls back to dynamic).

If you look at the @Html.DisplayNameFor directive, then the Html instance is an object of the type HtmlHelper<TModel> where TModel is the type given by the @model directive. In your case is the concrete type HtmlHelper<LoginViewModel>.

Now the HtmlHelper<LoginViewModel>.DisplayNameFor method is stongly typed and the compiler can figure that 'm' (which is only a parameter name) is of type LoginViewModel and that the lamdba expression returns a value from the model.

During runtime the DisplayNameFor method is executed by providing your model object as parameter 'm' the expression returns the object of the model member (or the object the expression returns) and the MVC framework can inspect the object (Type, Validation Attributes, etc.) and produces the appropriate html based on internal or custom templates.

If you would just pass a string, then MVC would not be able to get the needed type and validation annotations (and much more information).

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
0

Your first question: It passes LogonViewModel to @Html.DislplayNameFor method since you have define it on you first line as your model (@model LoginViewModel)

Aslo as it mentioned here: What is the @Html.DisplayFor syntax for?

Html.DisplayFor() will render the DisplayTemplate that matches the property's type.

If it can't find any, I suppose it invokes .ToString().

Community
  • 1
  • 1
Yar
  • 7,020
  • 11
  • 49
  • 69
0

DisplayNameFor is a strongly typed HTML helper. These were first introduced in ASP.NET MVC 2. The purpose of them (as per the linked article) is to...

...provide a nice way to get better type-safety within your view templates. This enables better compile-time checking of your views (allowing you to find errors at build-time instead of at runtime), and also supports richer intellisense when editing your view templates within Visual Studio.

If you want to understand the innards of how they work, the source code is available on GitHub.

Richard Ev
  • 52,939
  • 59
  • 191
  • 278