asp.net-mvc MVC和Razor中Html.TextboxFor和Html.EditorFor的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4826173/
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
Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor
提问by ShaneKm
Why by default were these changed when adding a new "edit" view? What are advantages when using EditorFor()
vs. TextboxFor()
?
为什么默认情况下在添加新的“编辑”视图时会更改这些内容?使用EditorFor()
vs.时有什么优势TextboxFor()
?
I found this
我找到了这个
By default, the Create and Edit scaffolds now use the Html.EditorFor helper instead of the Html.TextBoxFor helper. This improves support for metadata on the model in the form of data annotation attributes when the Add View dialog box generates a view.
默认情况下,创建和编辑脚手架现在使用 Html.EditorFor 助手而不是 Html.TextBoxFor 助手。当“添加视图”对话框生成视图时,这改进了对数据注释属性形式的模型元数据的支持。
采纳答案by Darin Dimitrov
The advantages of EditorFor
is that your code is not tied to an <input type="text"
. So if you decide to change something to the aspect of how your textboxes are rendered like wrapping them in a div
you could simply write a custom editor template (~/Views/Shared/EditorTemplates/string.cshtml
) and all your textboxes in your application will automatically benefit from this change whereas if you have hardcoded Html.TextBoxFor
you will have to modify it everywhere. You could also use Data Annotations to control the way this is rendered.
的优点EditorFor
是您的代码不绑定到<input type="text"
. 所以,如果你决定要改变一些东西到你的文本框的呈现方式就像一个包裹它们的方面div
,你可以简单地写一个自定义编辑模板(~/Views/Shared/EditorTemplates/string.cshtml
),并在您的应用程序将自动从这一变化有利于所有的文本框,而如果你已经硬编码了Html.TextBoxFor
你将不得不在任何地方修改它。您还可以使用数据注释来控制呈现的方式。
回答by Harshal
TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control.
TextBoxFor:它将呈现与指定表达式对应的文本输入 html 元素。简而言之,无论与控件绑定的属性的数据类型如何,它都将始终呈现为输入文本框。
EditorFor: This control is bit smart. It renders HTML markup based on the datatype of the property. E.g. suppose there is a boolean property in model. To render this property in the view as a checkbox either we can use CheckBoxFor or EditorFor. Both will be generate the same markup.
EditorFor:这个控件有点聪明。它根据属性的数据类型呈现 HTML 标记。例如,假设模型中有一个布尔属性。要在视图中将此属性呈现为复选框,我们可以使用 CheckBoxFor 或 EditorFor。两者都将生成相同的标记。
What is the advantage of using EditorFor?
使用 EditorFor 有什么好处?
As we know, depending on the datatype of the property it generates the html markup. So suppose tomorrow if we change the datatype of property in the model, no need to change anything in the view. EditorFor control will change the html markup automatically.
我们知道,根据属性的数据类型,它会生成 html 标记。所以假设明天如果我们更改模型中属性的数据类型,则无需更改视图中的任何内容。EditorFor 控件将自动更改 html 标记。
回答by GvS
The Html.TextboxFor
always creates a textbox (<input type="text" ...
).
将Html.TextboxFor
始终创建一个文本框(<input type="text" ...
)。
While the EditorFor looks at the type and meta information, and can render another control or a template you supply.
而 EditorFor 查看类型和元信息,并且可以呈现您提供的另一个控件或模板。
For example for DateTime properties you can create a template that uses the jQuery DatePicker.
例如,对于 DateTime 属性,您可以创建一个使用 jQuery DatePicker 的模板。
回答by Lalitha1729
This is one of the basic differences not mentioned in previous comments:Readonly
property will work with textbox for and it will not work with EditorFor
.
这是之前评论中没有提到的基本区别之一:Readonly
属性将与文本框一起使用,而不能与EditorFor
.
@Html.TextBoxFor(model => model.DateSoldOn, new { @readonly = "readonly" })
Above code works, where as with following you can't make control to readonly.
上面的代码有效,与以下一样,您无法控制readonly。
@Html.EditorFor(model => model.DateSoldOn, new { @readonly = "readonly" })
回答by Greg Gum
There is also a slight difference in the html output for a string data type.
字符串数据类型的 html 输出也略有不同。
Html.EditorFor:
<input id="Contact_FirstName" class="text-box single-line" type="text" value="Greg" name="Contact.FirstName">
Html.TextBoxFor:
<input id="Contact_FirstName" type="text" value="Greg" name="Contact.FirstName">