In ASP.NET MVC HtmlHelper class generates Html elements using the model class object in razor view.
@Html is an object of HtmlHelper class. (@ symbol is used to access a server-side object in razor syntax).
Html is a property of type HtmlHelper included in the base class of razor view WebViewPage.
ActionLink() and DisplayNameFor() is extension methods included in HtmlHelper class.
1.HtmlHelper - TextBox
HtmlHelper class includes two extension methods that create a textbox () element in razor view:
TextBox() and TextBoxFor().
The TextBox() method is loosely typed method whereas TextBoxFor() is a strongly typed method.
TextBox() =
The Html.TextBox() method creates element with specified name, value and html attributes.
TextBoxFor() =
TextBoxFor helper method is a strongly typed extension method.
It generates a text input element for the model property specified using a lambda expression.
TextBoxFor method binds a specified model object property to input text.
So it automatically displays a value of the model property in a textbox and visa-versa.
$. Difference between TextBox and TextBoxFor
1.@Html.TextBox() is a loosely typed method whereas @Html.TextBoxFor() is a strongly typed (generic) extension method.
2.TextBox() requires property name as string parameter whereas TextBoxFor() requires lambda expression as a parameter.
3.TextBox doesn't give you a compile-time error if you have specified the wrong property name. It will throw a run time exception.
4 .TextBoxFor is a generic method so it will give you a compile-time error if you have specified the wrong property name or property name changes.
(The provided view is not compiled at run time. )
2 . HtmlHelper -TextArea
HtmlHelper class includes two extension methods to generate a multi line element in a razor view: TextArea() and TextAreaFor(). By default, it creates textarea with rows=2 and cols=20.
TextArea() = The Html.TextArea() method creates <textarea rows="2" cols="20" element with specified name, value and html attributes.
TextAreaFor() =
TextAreaFor helper method is a strongly typed extension method.
It generates a multi-line element for the property in the model object specified using a lambda expression. TextAreaFor method binds a specified model object property to textarea element. So it automatically displays a value of the model property in a textarea and visa-versa.
HtmlHelper - CheckBox
HtmlHelper class includes two extension methods to generate a
element in razor view: CheckBox() and CheckBoxFor(). CheckBox() =
The Html.CheckBox() is a loosely typed method which generates a with the specified name,
isChecked boolean and html attributes.
CheckBoxFor()
CheckBoxFor helper method is a strongly typed extension method. It generates
element for the model property specified using a lambda expression.
CheckBoxFor method binds a specified model object property to the checkbox element.
So it automatically checked or unchecked a checkbox based on the property value.
4.HtmlHelper - RadioButton
HtmlHelper class include two extension methods to generate an element in a razor view:
RadioButton() and RadioButtonFor().
RadioButton()
The Html.RadioButton() method creates a radio button element with a specified name, isChecked boolean and Html attributes.
RadioButtonFor()
RadioButtonFor helper method is a strongly typed extension method.
It generates element for the property specified using a lambda expression.
RadioButtonFor method binds a specified model object property to RadioButton control.
So it automatically checked or unchecked a RadioButton based on the property value.
- HtmlHelper - DropDownList
In ASP.NET MVC HtmlHelper class includes two extension methods to generate a element in a razor view: DropDownList() and DropDownListFor().
DropDownList() The Html.DropDownList() method generates a select element with specified name, list items and html attributes.
DropDownListFor() DropDownListFor helper method is a strongly typed extension method. It generates element for the property specified using a lambda expression.
DropDownListFor method binds a specified model object property to the dropdown list control.
So it automatically lists items in DropDownList based on the property value.
HtmlHelper - Hidden field
HtmlHelper class includes two extension methods to generate a hidden field () element in a razor view:
Hidden() and HiddenFor().
Hidden() =
The Html.Hidden() method generates a input hidden field element with specified name, value and html attributes. HiddenFor() =
HiddenFor helper method is a strongly typed extension method. It generates a hidden input element for the model property specified using a lambda expression. hidden method binds a specified model object property to.
So it automatically sets a value of the model property to hidden field and visa-versa.
HtmlHelper - Password
HtmlHelper class includes two extension methods to generate a password field () element in a razor view:
Password() and PasswordFor(). Password()
The Html.Password() method generates an input password element with a specified name, value and Html attributes. password()
passwordfor helper method is a strongly typed extension method.
It generates an element for the model object property specified using a lambda expression.
PasswordFor method binds a specified model object property to.
So it automatically sets a value of the model property to the password field and visa-versa.
- HtmlHelper - Display =HtmlHelper class includes two extension methods to generate html string : Display() and DisplayFor()
Display() =
The Html.Display() is a loosely typed method that generates a string in razor view for the specified property of the model.
Display() method Signature: MvcHtmlString Display(string expression)
DisplayFor=
DisplayFor helper method is a strongly typed extension method.
It generates an Html string for the model object property specified using a lambda expression.
DisplayFor() method Signature: MvcHtmlString DisplayFor(> expression).
HtmlHelper - Editor
ASP.NET MVC includes the method that generates HTML input elements based on the datatype.
The Editor() or EditorFor() extension method generates Html elements based on the data type of the model object's property.
Editor() =
Editor() method requires a string expression parameter to specify the property name.
It creates an Html element based on the data type of the specified property.
EditorFor()=
EditorFor() method is a strongly typed method. It requires the lambda expression to specify a property of the model object.
10. HtmlHelper - Label
In ASP.NET MVC HtmlHelper class includes two extension methods to generate html label : Label() and LabelFor().
Label() =
The Html.Label() method generates an element for a specified property of the model object.
LabelFor() =
LabelFor helper method is a strongly typed extension method.
It generates an Html label element for the model object property specified using a lambda expression.
HtmlHelper is a class that is introduced in ASP.NET MVC 2. It is used to create HTML controls programmatically. It provides built-in methods to generate controls on the view page.
* If you found this article useful, share it.*
Umesh Mahajan | Facebook
Nature's Routes (naturesroutes.blogspot.com)
SoftCodeLearner: ASP.NET MVC - Model View Controller
SoftCodeLearner: ASP.NET Interview questions and answers
Good job 👌 keep it up 👍
ReplyDelete