Canvas
Canvas

Reputation: 5897

What does the "for" standard for in the mvc html.LabelFor

I'm just wondering what does the "For" in the .LabelFor or .EditorFor in the mvc html helper extension mean? I understand the parameter takes in a lambda expression but I can't work out what the "For" means?

Here is my simple cshtml file so you can see what I am looking at

@model MvcTest.Models.Update

@{
    ViewBag.Title = "Edit";
}

<h1>Update</h1>

@using (Html.BeginForm()) {
@Html.ValidationSummary()
<fieldset>
    <legend>Update profile</legend>

    @Html.HiddenFor(model => model.ID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
    </div>
    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

Upvotes: 0

Views: 243

Answers (2)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Html.LabeFor() returns an HTML label element and the property name of the property that is represented by the specified expression.

For means to bind the control with that specific Model Property in strongly typed views.

For Example:

I have a model with only two properties.

public class MyModel
{

public string Name {get;set;}
public int Id { get; set;}

}

Now we create a view which is strongly typed:

@model AppNameSpace.Models.MyModel

@using (Html.BeginForm("MyAction","My",FormMethod.Post)) 
{

@Html.TextBoxFor(m=>m.Name)

@Html.HiddenFor(m=>m.Id)

<input type="submit" name="Save" value="Save"/>

}

Now in the from the model object will be posted with values of form elements in model properties.

public class MyController : Controller
{


  [HttpGet]
  public Action Result SomeAction()
  {

   return View();

  }

  [HttpPost]
  public Action Result SomeAction(MyModel model)
  {

   string name = model.Name;// here you will have name value which was entered in textbox

   return View();

  }

}

If i say:

Html.TextBoxFor(x=>x.Name)

now when the from will be posted at the action with model , the text box value will be posted in the Name property whatever we have entered in the textbox. This is how strongly typed views work in asp.net mvc.

The same is the case for other Html helpers like Html.LabelFor(), Html.HiddenFor they are mostly used in strongly typed views, to reflect the values of from elements in action on form post of model.

For furthers detailed study,you might want to read more about Html Helpers here:

http://stephenwalther.com/archive/2009/03/03/chapter-6-understanding-html-helpers

http://www.dotnet-tricks.com/Tutorial/mvc/N50P050314-Understanding-HTML-Helpers-in-ASP.NET-MVC.html

https://www.simple-talk.com/dotnet/asp.net/writing-custom-html-helpers-for-asp.net-mvc/

Upvotes: 4

Daniel Imms
Daniel Imms

Reputation: 50149

The for is referring to the property that you're creating it on. @Html.LabelFor(model => model.Title) is a label for the Title field on the model. More specifically, it's for the Expression that you provide it.

Upvotes: 1

Related Questions