Anda di halaman 1dari 3

rkw

5,074 2 11 27
5 Answers
Jason
126k 14 208 371
Going from ASP.NET 2.0 (VB) to MVC 3 (C#), I'm very confused about the syntax being used for the View.
@Html.LabelFor(m => m.UserName)
Where did that m come from? My only guess is that it represents the model that is being passed into the
view. I tried changing the m to c and it still works fine.
Is the part of the syntax that involves the "=>" more of a MVC, C#, or Razor element?
c# asp.net-mvc razor
asked Jul 20 '11 at 17:30
@NoProblemBabe, there is someone doing it here: stackoverflow.com/questions/2595947/asp-net-mvc-label-for
user971077 Sep 29 '11 at 12:13
Where did that m come from?
It's the parameter in a lambda expression.
My only guess is that it represents the model that is being passed into the view. I tried changing the m
to c and it still works fine.
That's because the name doesn't matter. It's just a parameter name, it doesn't actually refer to any existing
variable.
Is the part of the syntax that involves the "=>" more of a MVC, C#, or Razor element?
It's C#, but LabelFor uses what the compiler translates m => m.UserName into to extract what it
needs to build up the label.
This is a very deep an intricate subject. I suggest that you find a book that you're comfortable with (e.g.,
C# in Depth is very good on this subject) to understand more. You want to read about lambda expressions
and expression trees.
answered Jul 20 '11 at 17:34
I don't he needs a book for it. Tocco Jul 20 '11 at 17:36
1 +1 on C# In Depth - Jon Skeet loves you... Matthew Abbott Jul 20 '11 at 17:46
It's a syntax trick that has been present since C# 3.0 (I think; maybe 3.5).
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
Syntax Question: @Html.LabelFor(m => m.UserName)
sign up log in tour help careers 2.0

c# - Syntax Question: @Html.LabelFor(m => m.UserName) - Stack Ove... https://stackoverflow.com/questions/6765959/syntax-question-html-labe...
1 of 3 14/08/2014 13:34
Vilx-
38k 32 142 281
NoProblemBabe
996 12 34
Sergey Metlov
6,545 8 36 87
If you were to write this in code (and your Razor view does get translated to a C# code file before
compiling, so it really is in code), there are two possible ways the compiler can compile it depending on the
context.
If the method LabelFor() expects a delegate, it's compiled to an anonymous method. Alternatively, if
the method expects an System.Linq.Expressions.Expression<Func> type, an expression tree is
constructed. This is what is happening in your case.
The reason for this convulted syntax is that the expression tree contains enough information, that
(combined with Reflection) the LabelFor() method can extract the actual property to which you are
referring. If you were to pass it simply as LabelFor(Model.UserName) , there wouldn't be enough
information for the LabelFor() to do this. It would just get the value from the UserName property. But
now it knows where it came from, and can use more Reflection to have a glance at the attributes of the
property. Attributes such as DisplayFormat , Required , and others.
The m (or c or whatever) is actually your model. LabelFor is an extension method and it simply
passes your model back to your Lambda expression, so that the whole expression tree trick can work. You
could also write it like LabelFor(x=>Model.UserName) , but I don't think it would work (I haven't tried
though, maybe it does).
answered Jul 20 '11 at 17:41
Ah, thanks for the explaining the reason behind the extended syntax. rkw Jul 20 '11 at 17:49
Lambda expressions a syntax trick? They're a really important language feature! Mike Goodwin Sep 24 '13 at
21:21
Yes. It's a really important syntax trick. :) OK, well, I suppose a lot of language features could be called "syntax
tricks". I use the term to mean "something that can be done without using the feature, except it gets a lot more
tedious to write". For example, a lambda expression x=>x*2 could be replaced with the more ancient
delegate(int x) { return x*2; } syntax. Or, if it is used to construct a Expression<T> , then you could use
the explicit Expression methods to build the tree. Of course, the code becomes tedious to the point where it's
useless. But it still can be done. Vilx- Sep 24 '13 at 21:48
@MikeGoodwin - Similarly, a using(x) { ... } is a "syntax trick", because you can replace it with
try...finally . The same about foreach and for (because you can replace them with a while ), etc.
Vilx- Sep 24 '13 at 21:50
That's a Lambda Expression, link.
The deal is this: the m is a variable that receives the instance of the model in the given circunstance.
Inside the labelFor, it will call a compile-time created class, which has a method that does what you have
passed as an argument to the LabelFor.
A lambda expression can be switched by a delegate, with the exact same results, excepting a minor, really
minor performance boost, once.
The general idea is that you are passing a method to be execute somewhere in the LabelFor method.
ex: the method:
public void Dummy(Action<string> action)
{
if(iFeelLikeIt) {action("I feel Like it");}
}
Should be used as:
Dummy(msg => MessageBox.Show(msg));
edited Jul 20 '11 at 17:40 answered Jul 20 '11 at 17:33
This is Lambda Expression. From MSDN:
A lambda expression is an anonymous function that can contain expressions and statements, and can be
used to create delegates or expression tree types.
All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda
operator specifies the input parameters (if any) and the right side holds the expression or statement block.
The lambda expression x => x * x is read "x goes to x times x."
answered Jul 20 '11 at 17:35
The LabelFor is a Razor function that has knowledge of the Model internally. In other words, the model
c# - Syntax Question: @Html.LabelFor(m => m.UserName) - Stack Ove... https://stackoverflow.com/questions/6765959/syntax-question-html-labe...
2 of 3 14/08/2014 13:34
Moises Issi
1 1
resides inside the LabelFor function logic. When you pass the lambda expression, you are passing an
anonymous function to LabelFor so it knows what to do with its internal Model object to extract
information.
Typically you would pass a Model as parameter and LabelFor would use internal logic to extract the
information needed. But in this format, it is the opposite. The LabelFor knows the parameter internally (the
Model) but it doesn't know what to do with it. You tell it what to do by passing the Lambda function.
When you run the code, you pass the Lambda expression as parameter to LabelFor. LabelFor, takes the
lambda expression and passes it an internal copy of the Model object. The Lambda expression returns the
UserName property of the Model object, and the LabelFor uses it to build your HTML code.
edited Sep 24 '13 at 21:10 answered Sep 24 '13 at 20:59
Not the answer you're looking for? Browse other questions tagged c# asp.net-mvc
razor or ask your own question.
c# - Syntax Question: @Html.LabelFor(m => m.UserName) - Stack Ove... https://stackoverflow.com/questions/6765959/syntax-question-html-labe...
3 of 3 14/08/2014 13:34

Anda mungkin juga menyukai