Answer: These kind of Html Helpers comes into use when the HTML elements are created based on model properties in a strongly types view. What the statement means can be better demonstrated by an example -
Let us create a model say
Employee
namespace DemoModel
{
public class Employee
{
public int EmpID { get; set; }
public string EmpName { get; set; }
}
}
Now let us create a strongly typed Employee using the Html helpers as under
@model DemoModel.Employee
@using(@Html.BeginForm()){
<div>
@Html.DisplayFor(m => m.EmpID)
@Html.TextBoxFor(m=>m.EmpID)
</div>
<div>
@Html.DisplayFor(m => m.EmpName)
@Html.TextBoxFor(m=>m.EmpName)
</div>
}
From the above code snippet, it is clear that the strongly typed HTML helpers work on lambda expression where the model object is passed as a value to lambda expression and we can select the field or property from model object to be used to set the id, name and value attributes of the HTML helper. The above code snippet will be render to
<div>
<span id="EmpID" name="EmpID"> { EmpID value(s) }</span>
<input id="EmpID" name="EmpID" type="text" value="{ EmpID value(s) }" />
</div>
<div>
<span id="EmpName" name="EmpName"> { EmpName value(s) }</span>
<input id="EmpName" name="EmpName" type="text" value="{ EmpName value(s) }" />
</div>
Asked In: Many Interviews |
Alert Moderator