Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Question: @Html.LabelFor(m => m.UserName)

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?

like image 503
rkw Avatar asked Jul 20 '11 17:07

rkw


1 Answers

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.

like image 166
jason Avatar answered Oct 02 '22 19:10

jason