Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.Textbox VS Html.TextboxFor

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

like image 392
eyalb Avatar asked May 06 '11 08:05

eyalb


People also ask

What is the difference between HTML TextBox and HTML TextBoxFor?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.

What is HTML TextBoxFor?

TextBoxFor() The TextBoxFor<TModel, TProperty>() is the generic extension method that creates <input type="text"> control. The first type parameter is for the model class, and second type parameter is for the property.

What is the difference between TextBoxFor and Editorfor?

it's absolutly wrong answer, becuase the key difference is that Texbox returns input and editorfor returns your template where input is default template for editorfor.

How disable HTML TextBoxFor in MVC?

You can make the input 'read only' by using 'readonly'. This will let the data be POSTED back, but the user cannot edit the information in the traditional fashion. Keep in mind that people can use a tool like Firebug or Dragonfly to edit the data and post it back.


1 Answers

Ultimately they both produce the same HTML but Html.TextBoxFor() is strongly typed where as Html.TextBox isn't.

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

will both produce

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

So what does that mean in terms of use?

Generally two things:

  1. The typed TextBoxFor will generate your input names for you. This is usually just the property name but for properties of complex types can include an underscore such as 'customer_name'
  2. Using the typed TextBoxFor version will allow you to use compile time checking. So if you change your model then you can check whether there are any errors in your views.

It is generally regarded as better practice to use the strongly typed versions of the HtmlHelpers that were added in MVC2.

like image 136
David Glenn Avatar answered Sep 30 '22 17:09

David Glenn