c# - syntax question_ @html.labelfor(m =_ m.username) - stack overflow.pdf

Upload: reggaemusic

Post on 03-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 c# - Syntax Question_ @Html.LabelFor(m =_ m.UserName) - Stack Overflow.pdf

    1/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.

    ( )

    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 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 o f the syntax that involves the "=>" more of a MVC, C#, or Razor element?

    It's C#, but uses what the compiler translates 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. ToccoJul 20 '11 at 17:36

    1 +1 on C# In Depth - Jon Skeet loves you... Matthew AbbottJul 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

    Syntax Question: @Html.LabelFor(m => m.UserName) - Stack Ove... https://stackoverflow.com/questions/6765959/syntax-question-html-labe...

    3 14/08/2014 13:34

  • 8/11/2019 c# - Syntax Question_ @Html.LabelFor(m =_ m.UserName) - Stack Overflow.pdf

    2/3

    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 isin code), there are two possible ways the compiler can compile it depending on the

    context.

    If the method () expects a delegate, it's compiled to an anonymous method. Alternatively, if

    the method expects an type, an expression treeis

    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 () method can extract the actual propertyto which you are

    referring. If you were to pass it simply as () , there wouldn't be enough

    information for the () to do this. It would just get the value from the property. But

    now it knows where it came from, and can use more Reflection to have a glance at the attributes of theproperty. Attributes such as , , and others.

    The (or or whatever) is actually your model. 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 () , 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. rkwJul 20 '11 at 17:49

    Lambda expressions a syntax trick? They're a really important language feature! Mike GoodwinSep 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 "syntaxtricks". 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 could be replaced with the more ancient

    syntax. Or, if it is used to construct a , then you could use

    the explicit 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 is a "syntax trick", because you can replace it with

    . The same about and (because you can replace them with a ), 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 havepassed 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:

    ()

    ()(" ")

    Should be used as:

    ( ())

    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

    Syntax Question: @Html.LabelFor(m => m.UserName) - Stack Ove... https://stackoverflow.com/questions/6765959/syntax-question-html-labe...

    3 14/08/2014 13:34

  • 8/11/2019 c# - Syntax Question_ @Html.LabelFor(m =_ m.UserName) - Stack Overflow.pdf

    3/3

    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.

    Syntax Question: @Html.LabelFor(m => m.UserName) - Stack Ove... https://stackoverflow.com/questions/6765959/syntax-question-html-labe...

    3 14/08/2014 13:34