MVC和Razor中Html.TextboxFor和Html.EditorFor的区别 [英] Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor

查看:31
本文介绍了MVC和Razor中Html.TextboxFor和Html.EditorFor的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么默认情况下在添加新的编辑"视图时这些会发生变化?使用 EditorFor()TextboxFor() 相比有什么优势?

Why by default were these changed when adding a new "edit" view? What are advantages when using EditorFor() vs. TextboxFor()?

我发现了这个

默认情况下,创建和编辑脚手架现在使用 Html.EditorFor 助手而不是 Html.TextBoxFor 助手.这改进了对模型元数据的支持添加视图对话框生成视图时的数据注释属性.

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.

推荐答案

EditorFor 的优点是你的代码不依赖于 <input type="text".因此,如果您决定更改文本框的渲染方式,例如将它们包装在 div 中,您可以简单地编写一个自定义编辑器模板 (~/Views/Shared/EditorTemplates/string.cshtml) 并且您应用程序中的所有文本框都将自动受益于此更改,而如果您对 Html.TextBoxFor 进行了硬编码,则必须在任何地方对其进行修改.您还可以使用数据注释来控制其呈现方式.

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.

这篇关于MVC和Razor中Html.TextboxFor和Html.EditorFor的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆