-3

I am very new in .NET and I have the following doubt about these expression that I am finding in some views.

I have a view like this:

@model Vidly_v2.ViewModels.NewCustomerViewModel
@{
    /**/

    ViewBag.Title = "New";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>New Customer</h2>

@using (Html.BeginForm("Save", "Customers"))
{
    <div class="form-group">
        @Html.LabelFor(m => m.Customer.Name)
        @Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Customer.Birthdate)
        @Html.TextBoxFor(m => m.Customer.Birthdate, "{0:d MMM yyyy}", new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Customer.MembershipTypeId)
        @Html.DropDownListFor(m => m.Customer.MembershipTypeId, new SelectList(Model.MembershipTypes, "Id", "Name"), "Select Membership Type", new { @class = "form-control" })
    </div>
    <div class="checkbox">
        <label>
            @Html.CheckBoxFor(m => m.Customer.IsSubscribedToNewsletter) Subscribed to Newsletter?
        </label>
    </div>
    @Html.HiddenFor(m => m.Customer.Id)
    <button type="submit" class="btn btn-primary">Save</button>
}

As you can see in this vire I am using this model object:

@model Vidly_v2.ViewModels.NewCustomerViewModel

Into this view I use this model object in this way:

@Html.LabelFor(m => m.Customer.Name)
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })

that simply render this HTML code:

<label for="Customer_Name">Name</label>
<input class="form-control" data-val="true" data-val-length="The field Name must be a string with a maximum length of 255." data-val-length-max="255" data-val-required="The Name field is required." id="Customer_Name" name="Customer.Name" value="" type="text">

related to this model object property.

My doubt is: what exactly is and what represents this expression?

m => m.Customer.Name

I think that m means model object (correct me if I am doing wrong assertion) but I can't understand the meaning and the exact use of this expression.

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • `Customer` means the property name which defines navigation property to other class (e.g. `Customer`) and `Name` means target property which contained inside navigation property. You should add the model class to help explain this further. – Tetsuya Yamamoto Oct 24 '18 at 09:01
  • 1
    https://weblogs.asp.net/scottgu/asp-net-mvc-2-strongly-typed-html-helpers – Patrick Hofman Oct 24 '18 at 09:03
  • Its a lambda expression.Think of it as _for my model, which I will refer to as `m`, give me the `Name` property of the `Customer` property_ –  Oct 24 '18 at 09:04