Hi,
EditorFor
-If the property is typed as a primitive type (integer, string, and so on), the method renders an
HTML input element for a text box
DisplayFor
- If the property is typed as a primitive type (integer, string, and so on), the method renders a
string that represents the property value
e.g. If we have EmployeeModel as below -
public class EmployeeModel
{
[Display(Name = "Employee ID :")]
public int Id { get; set; }
[Display(Name = "Employee Name :")]
public string Name { get; set; }
[Display(Name = "DOJ :")]
[DisplayFormat(DataFormatString = "{0:dd-mm-yyyy}")]
public DateTime DateOfJoining { get; set; }
}
Then on view we have,
@using (Html.BeginForm())
{
<h2>EditorFor Implementation</h2>
@Html.LabelFor(m=>m.Id)
@Html.EditorFor(m=>m.Id)
@Html.LabelFor(m=>m.Name)
@Html.EditorFor(m => m.Name)
// This will render Input box with value of property
@Html.LabelFor(m=>m.DateOfJoining)
@Html.EditorFor(m => m.DateOfJoining)
<hr />
<h2>DisplayFor Implementation</h2>
@Html.LabelFor(m=>m.Id)
@Html.DisplayFor(m=>m.Id)
@Html.LabelFor(m=>m.Name)
@Html.DisplayFor(m=>m.Name)
// This will render string object with value formatted as in DisplayFormat if specified
@Html.LabelFor(m=>m.DateOfJoining)
@Html.DisplayFor(m=>m.DateOfJoining)
}
I am not sure if I can attach screenshot here for reference, but this what i get from view source for above code.
For EditorFor will render input control as below -
<label for="DateOfJoining">DOJ :</label><input class="text-box single-line" data-val="true" data-val-date="The field DOJ : must be a date." data-val-required="The DOJ : field is required." id="DateOfJoining" name="DateOfJoining" type="datetime" value="7/8/2015 12:00:00 AM" />
For DisplauFor will render string as below -
<label for="DateOfJoining">DOJ :</label>08-00-2015
Hope this helps.