Loading

ASP.NET MVC

What is TextBox HTML Helper in ASP.NET MVC?. The Complete ASP.NET MVC Developer Course 2023 [Videos].

In this Video, I am going to discuss TextBox HTML Helper in ASP.NET MVC Application and along the way, we will also discuss how to create TextArea using HTML Helper in ASP.NET MVC application. Please read our previous Video before proceeding to this Video where we discussed the basis of HTML Helpers in the ASP.NET MVC Application. As part of this Video, we are going to discuss the following pointers.

  1. How to Create TextBox using HTML Helper in MVC?
  2. TextBox() HTML Helper Method in ASP.NET MVC.
  3. TextBoxFor() HTML Helper Method in ASP.NET MVC application
  4. What are the Differences between Html.TextBox and Html.TextBoxFor in MVC Application?
  5. How to Create TexArea using HTML Helper in MVC?
  6. Understanding TextArea() and TextAreaFor() HTML Helper Method in MVC.
How to Create TextBox using HTML Helper in MVC?

To create a TextBox using HTML Helper Method in the ASP.NET MVC application, we need to use the TextBox Helper method. In the ASP.NET MVC application, we can use two different types of TextBox Helper methods to generates a textbox in a view. Those two extension methods are TextBox() and TextBoxFor(). The TextBox() HTML Helper method is a loosely typed method whereas the TextBoxFor() HTML Helper method is a strongly typed method.

Lets understand How to Create TextBox using HTML Helper in MVC with one example:

Lets create an ASP.NET MVC 5 application with the name HTML_HELPER. Then Create Employee.cs class file within the Models folder and then copy and paste the following code into it.

using System.ComponentModel.DataAnnotations;
namespace HTML_HELPER.Models
{
public class Employee
{
public int EmployeeId { get; set; }
[Display(Name = "Name")]
public string EmployeeName { get; set; }
public string Password { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public Nullable<decimal> Salary { get; set; }
}
}

We are going to use the above Employee model with TextBox() and TextBoxFor() HTML Helper methods. Once you create the Employee model next we are going to create an MVC 5 Empty Controller with the name EmployeeController within the Controllers Folder.

namespace HTML_HELPER.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}

The EmployeeController is created with one action method i.e. the Index action method. So, create the respective Index view with empty HTML.

TextBox() HTML Helper Method in ASP.NET MVC:

The Html.TextBox() Helper method creates an element of  with specified name, value and HTML attributes. There 7 overloaded versions of this Html.TextBox() Helper method is available as shown in the below image. The following method are loosely typed method

HTML TextBox Helper Method in ASP.NET MVC

Parameters:
  1. htmlHelper: The HTML helper instance that this method extends. It indicates that it is an extension method that belongs to HtmlHelper class.
  2. name: The name of the form field and the System.Web.Mvc.ViewDataDictionary key that is used to look up the value.
  3. value: The value of the text input element. The value is retrieved in this order – the System.Web.Mvc.ModelStateDictionary object, the value of this parameter, the System.Web.Mvc.ViewDataDictionary object, and lastly, a value attribute in the HTML attributes.
  4. format: A string that is used to format the input.
  5. htmlAttributes: An object that contains the HTML attributes to set for the element.

Returns: This method returns an input element whose type attribute is set to “text”.

Note: Replace Home with Employee in RouteConfig class. 

Modify the Index View:

Please modify the Index View as shown below to use the TextBox Helper method. 
@model HTML_HELPER.Models.Employee
@Html.TextBox(“EmployeeName”, null, new { @class = “form-control” })

Run the application and inspect the Html and you will see that it will produce the following HTML for the textbox.

In the above example, the first parameter is “EmployeeName” which is a property of the Employee model which will be set as the name and id of the textbox. The second parameter is the value that we need to display in the textbox, which is null in the above example because the TextBox() method will automatically display the value of the EmployeeName property of the Employee model. The third parameter will be set as the class attribute. The HTML attributes parameter is an object type, so it can be an anonymous object, and the attributes name will be its properties starting with @ symbol.

You can also specify any name for the textbox. However, it will not be bind to a model.
@Html.TextBox(“myTextBox”, “This is value”, new { @class = “form-control” })

It will produce the following HTML

TextBoxFor() HTML Helper Method in ASP.NET MVC application:

The TextBoxFor() HTML Helper method is a strongly typed extension method. It generates an element of for the model property which needs to be specified using a lambda expression. The TextBoxFor() HTML Helper method binds the specified model object property to the input element. So it automatically displays that model property value within the textbox. The TextBoxFor() HTML Helper Method has 6 overloaded versions available in ASP.NET MVC Framework are shown in the below image.

TextBoxFor HTML Helper Method in ASP.NET MVC

Parameters:
  1. htmlHelper: The HTML helper instance that this method extends.
  2. expression: An expression that identifies the object that contains the properties to display.
  3. format: A string that is used to format the input.
  4. htmlAttributes: An object that contains the HTML attributes to set for the element.

Type parameters:

  1. TModel: The type of model.
  2. TProperty: The type of the value.

Returns: An input element whose type attribute is set to “text”.

Example to understand the TextBoxFor() HTML Helper Method in MVC:

Copy and Paste the following code in Index.cshtml view
@model HTML_HELPER.Models.Employee
@Html.TextBoxFor(m => m.EmployeeName, new { @class = “form-control” })

Run the application and insepct the element and you will see that, it will produce the following Html

In the above example, the first parameter in TextBoxFor() HTML Helper Method is a lambda expression which specifies the EmployeeName property of the Model object to bind with the textbox. It generates an input type text element with id and name property and both the properties value are set to EmployeeName. The value attribute will be set to the value of the EmployeeName property of the Model Object.

What are the Differences between Html.TextBox and Html.TextBoxFor in ASP.NET MVC application?

As we already discussed that the @Html.TextBox() is a loosely typed helper method whereas the @Html.TextBoxFor() is a strongly typed helper method.

The Html.TextBox() Helper method is not strongly typed and hence they dont require a strongly typed view. This means that we can hardcode whatever name we want. On the other hand the Html.TextBoxFor() HTML Helper method is a strongly typed method and hence it requires a strongly typed view and the name should be given using the lambda expression. What is a strongly typed view that we will discuss in our upcoming Videos?

The Strongly typed HTML helper methods also provide compile-time error checking. In real-time applications, we mostly prefer to use strongly typed HTML Helper methods. 

But the most important point that we need to keep in mind is whether we use Html.TextBox Helper method or Html.TextBoxFor() Helper method the end result is the same that is they generate the same HTML.

Note: The Strongly typed HTML helpers are introduced in MVC2. 

How to Create TexArea using HTML Helper in ASP.NET MVC Application?

To create TextArea using HTML Helper in ASP.NET MVC application, we need to use the TextArea Helper methods.  The HtmlHelper class provides two extension methods to generate the textarea i.e. multiline textbox in a razor view: They are TextArea() and TextAreaFor(). By default, the textarea is created with rows=2 and cols=20 size.

TextArea() HTML Helper Method in MVC:

The Html.TextArea() method creates an input HTML element of 

See All

Comments (292 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /292

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /292

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /292