Html.Textbox VS Html.TextboxFor

What is the difference between Html.Textbox and Html.TextboxFor?

+83
asp.net-mvc asp.net-mvc-3 razor
May 6 '11 at 8:04
source share
4 answers

Ultimately, both of them produce the same HTML, but Html.TextBoxFor () is strongly typed, where Html.TextBox is not.

1: @Html.TextBox("Name") 2: Html.TextBoxFor(m => m.Name) 

will produce

 <input id="Name" name="Name" type="text" /> 

So what does this mean in terms of use?

Typically, two things:

  • TextBoxFor will generate your names for you. This is usually just a property name, but for complex type properties, it may include an underscore such as "computer_name"
  • Using a typed version of TextBoxFor will allow you to use compile-time checking. Therefore, if you change your model, you can check if there are any errors in your views.

Generally, it is best to use strongly typed versions of HtmlHelpers that have been added to MVC2 .

+102
May 6 '11 at 8:15
source share

TextBoxFor is the new MVC input extension introduced in MVC2.

The main advantage of the new strongly typed extensions is the display of any errors / warnings at compile time, rather than run time.

See this page.

http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx

+15
May 6 '11 at 8:08
source share

IMO the main difference is that the text box is not very typed. Text field. Take lambda as a parameter that tells the helper the element with the model to use in a typed way.

You can do the same with both, but you should use typed views and the "If possible" text box.

+6
May 6 '11 at 8:13 a.m.
source share

Html.TextBox amd Html.DropDownList are not strongly typed and therefore they do not require a strongly typed representation. This means that we can hardcode any name we want. On the other hand, Html.TextBoxFor and Html.DropDownListFor are strongly typed and require a strongly typed representation, and this name is derived from the lambda expression.

Strongly configured HTML helpers also provide compile-time checking.

Since in real time we mainly use strongly typed views, we prefer to use Html.TextBoxFor and Html.DropDownListFor for our colleagues.

Whether we use Html.TextBox and Html.DropDownList OR Html.TextBoxFor and Html.DropDownListFor, the end result is the same, that is, they create the same HTML.

Strongly typed HTML helpers have been added to MVC2.

+3
Apr 16 '15 at 3:32
source share



All Articles