如何重写 EditorFor 布局
如何覆盖EditorFor布局?例如,如果我想在编辑器字段之前或之后渲染一些文本。
How to override EditorFor layout? For example if i want to render some text before or after editor field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是在 EditorFor 之前和之后添加一些文本的代码示例。
步骤 1:使用特定的 DataType 在模型类中定义字段,并使用听起来合乎逻辑的自定义名称,如 SmallText、CustomName 等。在本例中我仅使用了“MyTypeOfField”。
步骤 2:使用以下代码在文件夹:Views/Shared/EditorTemplates 中创建“MyTypeOfField.cshtml”。如果 EditorTemplates 文件夹不存在,请创建它。
希望这有帮助!
Here is the code sample to add some text before and after EditorFor.
Step 1: Define field in model class with a specific DataType with a logical sounding custom names like SmallText, CustomName etc. I just used "MyTypeOfField" in this example.
Step 2: Create "MyTypeOfField.cshtml" in folder : Views/Shared/EditorTemplates with below code. If EditorTemplates folder is not present, create it.
Hope this helps!
EditorFor
是在HtmlHelper
类上定义的扩展方法。如果你想覆盖它,你必须子类化HtmlHelper
。根据此讨论,覆盖扩展方法是一个坏主意。即使您决定采用这种方式,如何让 mvc 使用子类HtmlHelper
而不是默认的。您最好的选择可能是重载EditorFor
方法并在视图中使用它而不是默认视图。EditorFor
is an extension method defined onHtmlHelper
class. if you want to override it you have to subclassHtmlHelper
. according to this discussion overriding extension methods is a bad idea. Even if you decide to go this way how would you make mvc use subclassedHtmlHelper
instead of default one. your best bet could be overloadingEditorFor
method and using it in views instead of default ones.