difference between Html.TextBox and Html.TextBoxFor

白昼怎懂夜的黑 提交于 2019-12-23 07:02:35

问题


What is the difference between Html.TextBox and Html.TextBoxFor? As far as I know they produce same html output. Unless I'm missing something here. Please help me with this.


回答1:


Html.TextBox is not strongly typed and it doesn't require a strongly typed view meaning that you can hardcode whatever name you want as first argument and provide it a value:

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

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

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

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

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

And because it is a good practice to use strongly typed views and view models you should always use the Html.TextBoxFor helper.




回答2:


Strongly typed extensions is to show any errors or warnings at compile time rather than run time. Please see this page, it will clarify all your doubts



来源:https://stackoverflow.com/questions/5119373/difference-between-html-textbox-and-html-textboxfor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!