What are the differences between Html.TextBox and Html.TextBoxFor?

  • Html.TextBox and Html.DropDownList are not Strongly typed and hence they doesn’t require a Strongly typed View. This means we can hard code whatever name we want. On the other hand, Html.TextBoxFor and Html.DropDownListFor are Strongly typed, so require a Strongly typed View and the name is inferred from thelambda expression.

Example

@Html.TextBox(“Name”)
@Html.TextBoxFor(m => m.Name)

  • Whether we use Html.TextBox or  Html.TextBoxFor, the end result will be the same, that is they will produce the same HTML.

Example

The above 2 lines of code will produce same HTML as shown below.

<input id=“Name” name=“Name” type=”text”/>

  • Strongly typed HTML helpers provide compile time error checking which is not provided by the other.
  • Since it is a good practice to use Strongly typed Views, it is always preferred to use Html.TextBoxFor and Html.DropDownListFor over their counterparts.
  • Strongly typed HTML helpers are added in ASP.Net MVC 2.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s