@Html.EditorFor(m => m) lambda syntax in MVC

前端 未结 4 1437
野趣味
野趣味 2021-02-05 16:08

I\'m just learning C# and MVC, and trying to understand some examples.

@Html.EditorFor(m => m)

Eventually I figured out that \'=>\' is the l

4条回答
  •  忘了有多久
    2021-02-05 16:16

    1. In your example, the lambda function doesn't serve any purpose, true. But its real use is for when you want to render the editor for a property of the model: @Html.EditorFor(m => m.SomeProperty). Lambda expressions are really just shorthand for functions, but strongly typed by way of delegates. The concept might be more familiar from Javascript:

      myFunction( function(x) { return x.SomeProperty; });

    2. The "m" isn't predefined. It's naming the parameter for the second part of the lambda expression, and could be anything: @Html.EditorFor( whatever => whatever ). (It refers to the same thing, in this case the page's model, regardless of what you name it.)

    3. The first parameter you see in those definitions for Html.EditorFor isn't really a parameter. You'll notice they're using the this keyword to define extension methods. That parameter refers the the object that invoked the method, in this case the HtmlHelper object.

提交回复
热议问题