top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?

+2 votes
991 views
What is the difference between “HTML.TextBox” vs “HTML.TextBoxFor”?
posted Jul 6, 2015 by Shivaranjini

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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 the lambda 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.

answer Jul 7, 2015 by Manikandan J
...