asp.net-mvc Html.Textbox VS Html.TextboxFor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5908523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Html.Textbox VS Html.TextboxFor
提问by eyalb
What is the difference between Html.Textbox and Html.TextboxFor?
Html.Textbox 和 Html.TextboxFor 有什么区别?
回答by David Glenn
Ultimately they both produce the same HTML but Html.TextBoxFor()is strongly typed where as Html.TextBoxisn't.
最终它们都生成相同的 HTML,但Html.TextBoxFor()是强类型的,而Html.TextBox不是。
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:
一般有两点:
- 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' - 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.
- typed
TextBoxFor
将为您生成输入名称。这通常只是属性名称,但对于复杂类型的属性可以包含下划线,例如“customer_name” - 使用类型化
TextBoxFor
版本将允许您使用编译时检查。因此,如果您更改模型,则可以检查视图中是否有任何错误。
It is generally regarded as better practice to use the strongly typed versions of the HtmlHelpers that were added in MVC2.
回答by iain
The TextBoxFor
is a newer MVC input extension introduced in MVC2.
这TextBoxFor
是 MVC2 中引入的较新的 MVC 输入扩展。
The main benefit of the newer strongly typed extensions is to show any errors / warnings at compile-time rather than runtime.
较新的强类型扩展的主要好处是在编译时而不是运行时显示任何错误/警告。
See this page.
请参阅此页面。
http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx
http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx
回答by Jonathan
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.
IMO 的主要区别在于 Textbox 不是强类型的。TextboxFor 将 lambda 作为参数,告诉助手模型的 with 元素在类型化视图中使用。
You can do the same things with both, but you should use typed views and TextboxFor when possible.
您可以对两者执行相同的操作,但应尽可能使用类型化视图和 TextboxFor。
回答by Yogesh Yadav
Html.TextBox amd Html.DropDownList are not strongly typed and hence they doesn't require a strongly typed view. This means that we can hardcode whatever name we want. On the other hand, Html.TextBoxFor and Html.DropDownListFor are strongly typed and requires a strongly typed view, and the name is inferred from the lambda expression.
Html.TextBox 和 Html.DropDownList 不是强类型的,因此它们不需要强类型的视图。这意味着我们可以硬编码我们想要的任何名称。另一方面,Html.TextBoxFor 和 Html.DropDownListFor 是强类型的,需要强类型的视图,名称是从 lambda 表达式推断出来的。
Strongly typed HTML helpers also provide compile time checking.
强类型 HTML 帮助程序还提供编译时检查。
Since, in real time, we mostly use strongly typed views, prefer to use Html.TextBoxFor and Html.DropDownListFor over their counterparts.
由于在实时中,我们主要使用强类型视图,因此更喜欢使用 Html.TextBoxFor 和 Html.DropDownListFor 而不是它们的对应物。
Whether, we use Html.TextBox & Html.DropDownList OR Html.TextBoxFor & Html.DropDownListFor, the end result is the same, that is they produce the same HTML.
无论是使用 Html.TextBox & Html.DropDownList 还是 Html.TextBoxFor & Html.DropDownListFor,最终结果都是一样的,即它们产生相同的 HTML。
Strongly typed HTML helpers are added in MVC2.
MVC2 中添加了强类型 HTML 助手。