如何重写 EditorFor 布局

发布于 2024-12-23 19:00:49 字数 48 浏览 3 评论 0原文

如何覆盖EditorFor布局?例如,如果我想在编辑器字段之前或之后渲染一些文本。

How to override EditorFor layout? For example if i want to render some text before or after editor field.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-12-30 19:00:49

以下是在 EditorFor 之前和之后添加一些文本的代码示例。

步骤 1:使用特定的 DataType 在模型类中定义字段,并使用听起来合乎逻辑的自定义名称,如 SmallText、CustomName 等。在本例中我仅使用了“MyTypeOfField”。

[Required(ErrorMessage = "Field_Value code is a required field.")]
    [MaxLength(100)]
    [Display(Name = "Field Display Name")]
    [DataType("MyTypeOfField")]
    public string MyField { get; set; }

步骤 2:使用以下代码在文件夹:Views/Shared/EditorTemplates 中创建“MyTypeOfField.cshtml”。如果 EditorTemplates 文件夹不存在,请创建它。

@inherits System.Web.Mvc.WebViewPage<string>
@string.Format("{0, -10}","XXXXXXXXXX") @Html.TextBox("", "", new { style = "width:30px; text-align:right;" }) @string.Format("{0, -10}","1234567890")

希望这有帮助!

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.

[Required(ErrorMessage = "Field_Value code is a required field.")]
    [MaxLength(100)]
    [Display(Name = "Field Display Name")]
    [DataType("MyTypeOfField")]
    public string MyField { get; set; }

Step 2: Create "MyTypeOfField.cshtml" in folder : Views/Shared/EditorTemplates with below code. If EditorTemplates folder is not present, create it.

@inherits System.Web.Mvc.WebViewPage<string>
@string.Format("{0, -10}","XXXXXXXXXX") @Html.TextBox("", "", new { style = "width:30px; text-align:right;" }) @string.Format("{0, -10}","1234567890")

Hope this helps!

若能看破又如何 2024-12-30 19:00:49

EditorFor 是在 HtmlHelper 类上定义的扩展方法。如果你想覆盖它,你必须子类化HtmlHelper。根据此讨论,覆盖扩展方法是一个坏主意。即使您决定采用这种方式,如何让 mvc 使用子类 HtmlHelper 而不是默认的。您最好的选择可能是重载 EditorFor 方法并在视图中使用它而不是默认视图。

EditorFor is an extension method defined on HtmlHelper class. if you want to override it you have to subclass HtmlHelper. 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 subclassed HtmlHelper instead of default one. your best bet could be overloading EditorFor method and using it in views instead of default ones.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文