Please clarify the use of & ldquo; = & Gt; & Rdquo; In the MVC Razor syntax?

advertisements

This question already has an answer here:

  • What does the '=>' syntax in C# mean? 7 answers
  • MVC HTML Helpers and Lambda Expressions 2 answers

Please clarify the use of => in MVC Razor. For example, in the context of a statement such as:

@Html.LabelFor(m => m.AnswerText)


=> is a lambda expression. Basically it's short hand for selecting the value of one of the parameters in your model. For instance if you have

@model YourDomain.Foo.Models.Bar

and in your Bar model you have a string called UserName then using @Html.TextBoxFor(m => m.UserName) will display the value of the user name in the model. ie (John Smith) When using a LabelFor however instead of it displaying the value of the Model parameter it displays the name of the model parameter so in your example where you are using

@Html.LabelFor(m => m.AnswerText)

your resulting display will be AnswerText