在MVC和剃刀Html.TextboxFor和Html.EditorFor之间的差异 [英] Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor
问题描述
为什么在默认情况下被添加新的编辑视图时,这些变化?使用时有哪些优点 EditorFor()
与 TextboxFor()
?
我发现这个
默认情况下,创建和编辑支架现在使用的Html.EditorFor帮手,而不是Html.TextBoxFor帮手。这提高了对形式的模型元数据支持
当添加视图对话框生成视图数据标注属性。
块引用>解决方案
EditorFor
的好处是,你的code未绑定到<输入类型=文本
。所以,如果你决定要改变一些东西给你的文本框的呈现方式像在DIV
你可以简单地写一个自定义编辑模板(包裹它们的方面〜/查看/共享/ EditorTemplates / string.cshtml
)和所有的文本框在你的应用程序会自动从这种变化中受益,而如果你有硬codeDHtml.TextBoxFor
你将不得不到处修改。你也可以使用数据注释来控制这个被渲染的方式。Why by default were these changed when adding a new "edit" view? What are advantages when using
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.
解决方案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 adiv
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 hardcodedHtml.TextBoxFor
you will have to modify it everywhere. You could also use Data Annotations to control the way this is rendered.这篇关于在MVC和剃刀Html.TextboxFor和Html.EditorFor之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!