0

Please, can anyone tell me what the meaning of => operator in Html helper syntax.

Eg: @Html.TextBoxFor(m => m.UserName)

James McCormack
  • 9,217
  • 3
  • 47
  • 57

2 Answers2

5

It's not specific to the Razor engine. That is a C# lambda expression.

doogle
  • 3,376
  • 18
  • 23
4

m => m.Username is equivalent to providing a method, something like

string GetUserName(TypeOfModel m)
{
   return m.UserName;
}

However, because TextBoxFor takes an Expression, it is able to 'parse' the lambda and is able to infer that the name of the TextBox input should be 'Name' (viz the property scraped off the model). This is important and 'intuitive', since the MVC ModelBinder will then be able to map "Name" to a property or parameter when it is next sent back to a controller.

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285