Html.TextBox is an HTML helper method in ASP.NET MVC that generates a HTML input element of type "text". It provides an easy and flexible way to create and customize input text boxes in Razor views. It allows developers to set various HTML attributes such as ID, CSS class, size, default value, readonly, and max length, making it a powerful tool for building forms and user input interfaces in .NET MVC applications.
We will explore following examples:
- Add CSS Class to Html.Textbox
- Add Id to Html.Textbox
- Add HTML Attributes to Html.Textbox
- Add Required Attribute Html.Textbox
- Add Auto Focus to Html.Textbox
- Set Size of Html.Textbox
- Set Default value to Html.Textbox
- Make Html.Textbox Readonly
- Set Max Length of Html.Textbox
Add CSS Class to Html.Textbox
To add a CSS class to the HTML TextBox, we can use the
Html.TextBox
helper method and pass a
new { @class = "classname" }
anonymous object as the HTML attributes parameter. Here's an example:
@Html.TextBox("FirstName", null, new { @class = "form-control" })
Add Id to Html.Textbox
To add an ID to the HTML TextBox, we can use the
Html.TextBox
helper method and pass a
new { id = "textboxid" }
anonymous object as the HTML attributes parameter. Here's an example:
@Html.TextBox("FirstName", null, new { id = "firstname" })
Add HTML Attributes to Html.Textbox
To add custom HTML attributes to the HTML TextBox, we can use the
Html.TextBox
helper method and pass an anonymous object with the attribute names and values as the HTML attributes parameter. Here's an example:
@Html.TextBox("FirstName", null, new { data_bind = "value: firstName", placeholder = "Enter your first name" })
Add Required Attribute Html.Textbox
To add a Required attribute to the HTML TextBox, we can use the
Html.TextBox
helper method and add a
data-val-required
attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { data_val_required = "The First Name field is required." })
@Html.ValidationMessage("FirstName")
Add Auto Focus to Html.Textbox
To set the Auto-focus on the HTML TextBox, we can use the
Html.TextBox
helper method and add a
autofocus
attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { autofocus = "autofocus" })
Set Size of Html.Textbox
To set the size of the HTML TextBox, we can use the
Html.TextBox
helper method and add a
size
attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { size = "30" })
Set Default value to Html.Textbox
To set a default value for the HTML TextBox, we can use the
Html.TextBox
helper method and add a
value
attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", "John")
Make Html.Textbox Readonly
To make the HTML TextBox readonly, we can use the
Html.TextBox
helper method and add a
readonly
attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { @readonly = "readonly" })
Set Max Length of Html.Textbox
To set the maximum length of the HTML TextBox, we can use the
Html.TextBox
helper method and add a
maxlength
attribute to the anonymous object as shown below:
@Html.TextBox("FirstName", null, new { maxlength = "50" })
In conclusion, the
Html.TextBox
method provides a flexible way to customize the HTML TextBox element in .NET MVC applications. By adding various HTML attributes, developers can easily customize the behavior and appearance of the HTML TextBox to meet their specific requirements.