Eduardo
Eduardo

Reputation: 5943

Get property name from it's value

How does Microsoft MVC know that the name of the property is "Phone" since we have only the object instance and one of it's properties' value?

<%: Html.TextBoxFor(x => x.Phone) %>

Upvotes: 0

Views: 441

Answers (2)

jeroenh
jeroenh

Reputation: 26792

They are using this 'trick'

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503300

You haven't got the property's value - you've got an expression tree which tells you how to obtain the value from an item. That expression tree can be analyzed by the framework to find the property name.

Now if it were

<%: Html.TextBoxFor(x.Phone) %>

then that would genuinely be just getting the value... but the lambda expression is being converted into an expression tree by the C# compiler.

Upvotes: 3

Related Questions