Html.TextBox和Html.TextBoxFor区别 [英] difference between Html.TextBox and Html.TextBoxFor
问题描述
什么是Html.TextBox和Html.TextBoxFor之间的区别?据我所知,他们产生相同的HTML输出。除非我失去了一些东西。请帮我这一点。
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.
推荐答案
Html.TextBox
不是强类型的,它并不需要一个强类型的视图这意味着你可以任何名称要作为第一个参数和硬code提供一个值:
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") %>
您可以设置在控制器的动作里面的的ViewData code>字典一定的价值和渲染文本框时,助手将使用该值(
计算机[富] =栏
)。
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
是需要一个强类型的视图,并使用视图模型:
Html.TextBoxFor
is requires a strongly typed view and uses the view model:
<%= Html.TextBoxFor(x => x.Foo) %>
助手将使用拉姆达前pression推断的名称和传递给视图的视图模式的价值。
The helper will use the lambda expression to infer the name and the value of the view model passed to the view.
和因为它是一个很好的做法,使用强类型的视图和视图模型,你应该始终使用 Html.TextBoxFor
帮手。
And because it is a good practice to use strongly typed views and view models you should always use the Html.TextBoxFor
helper.
这篇关于Html.TextBox和Html.TextBoxFor区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!