Difference between Html.TextBox and Html.TextBoxFor

What is the difference between Html.TextBox and Html.TextBoxFor? As far as I know, they produce the same html output. If I don’t miss something here. Please help me with this.

+12
asp.net-mvc-2
Feb 25 2018-11-15T00:
source share
2 answers

Html.TextBox not strongly typed and does not require a strongly typed representation, meaning that you can hardcode any name you want as the first argument and provide it with a value:

 <%= Html.TextBox("foo", "some value") %> 

You can set some value in the ViewData dictionary inside the controller action, and the assistant will use this value when rendering the text field ( ViewData["foo"] = "bar" ).

Html.TextBoxFor requires a strongly typed view and uses a view model:

 <%= Html.TextBoxFor(x => x.Foo) %> 

The helper will use the lambda expression to display the name and value of the view model passed to the view.

And since it's good to use strongly typed views and view models, you should always use the Html.TextBoxFor .

+21
Feb 25 2018-11-15T00:
source share

Strongly typed extensions are to show any errors or warnings at compile time, rather than run time. Check out this page to clarify all your doubts.

0
Sep 28 '16 at 20:14
source share



All Articles