想知道为什么在覆盖属性的 LabelFor 中忽略 DisplayName 属性 [英] Wondering why DisplayName attribute is ignored in LabelFor on an overridden property

查看:22
本文介绍了想知道为什么在覆盖属性的 LabelFor 中忽略 DisplayName 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我在 ASP.NET MVC 2 中执行几个 <%=Html.LabelFor(m=>m.MyProperty)%> 并使用 时感到困惑[DisplayName("Show this instead of MyProperty")] 属性来自 System.ComponentModel.

today I got confused when doing a couple of <%=Html.LabelFor(m=>m.MyProperty)%> in ASP.NET MVC 2 and using the [DisplayName("Show this instead of MyProperty")] attribute from System.ComponentModel.

事实证明,当我将属性放在被覆盖的属性上时,LabelFor 似乎没有注意到它.
但是,[Required] 属性在被覆盖的属性上工作正常,并且生成的错误消息实际上使用了 DisplayNameAttribute.

As it turned out, when I put the attribute on an overridden property, LabelFor didn't seem to notice it.
However, the [Required] attribute works fine on the overridden property, and the generated errormessage actually uses the DisplayNameAttribute.

这是一些琐碎的示例代码,更现实的情况是我有一个与视图模型分开的数据库模型,但为了方便起见,我想从数据库模型继承,添加仅查看属性并用属性装饰视图模型用于用户界面.

This is some trivial examplecode, the more realistic scenario is that I have a databasemodel separate from the viewmodel, but for convenience, I'd like to inherit from the databasemodel, add View-only properties and decorating the viewmodel with the attributes for the UI.

public class POCOWithoutDataAnnotations
{
    public virtual string PleaseOverrideMe { get; set; }        
} 
public class EditModel : POCOWithoutDataAnnotations
{
    [Required]
    [DisplayName("This should be as label for please override me!")]
    public override string PleaseOverrideMe 
    {
        get { return base.PleaseOverrideMe; }
        set { base.PleaseOverrideMe = value; }
    }

    [Required]
    [DisplayName("This property exists only in EditModel")]
    public string NonOverriddenProp { get; set; }
}

强类型ViewPage<EditModel>包含:

        <div class="editor-label">
            <%= Html.LabelFor(model => model.PleaseOverrideMe) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.PleaseOverrideMe) %>
            <%= Html.ValidationMessageFor(model => model.PleaseOverrideMe) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.NonOverriddenProp) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.NonOverriddenProp) %>
            <%= Html.ValidationMessageFor(model => model.NonOverriddenProp) %>
        </div>

标签随后显示为PleaseOverrideMe"(使用 DisplayNameAttribute)和此属性仅存在于 EditModel"(在查看页面时使用 DisplayNameAttribute).
如果我使用空值发布,则使用此 ActionMethod 触发验证:

The labels are then displayed as "PleaseOverrideMe" (not using the DisplayNameAttribute) and "This property exists only in EditModel" (using the DisplayNameAttribute) when viewing the page.
If I post with empty values, triggering the validation with this ActionMethod:

    [HttpPost]
    public ActionResult Edit(EditModel model)
    {
        if (!ModelState.IsValid)
            return View(model);
        return View("Thanks");
    }

<%= Html.ValidationMessageFor(model => model.PleaseOverrideMe) %> 实际上使用 [DisplayName("This should be as label for please override me!")] 属性,并产生默认的错误文本 "This should be as label for please override me! field is required."

the <%= Html.ValidationMessageFor(model => model.PleaseOverrideMe) %> actually uses [DisplayName("This should be as label for please override me!")] attribute, and produces the default errortext "The This should be as label for please override me! field is required."

会不会有一些友好的灵魂对此有所了解?

Would some friendly soul shed some light on this?

推荐答案

使用强类型帮助器的模型绑定和元数据查看模型的声明类型,而不是运行时类型.我认为这是一个错误,但显然 MVC 团队不同意我的观点,因为我在这方面的 Connect 问题已作为设计"关闭.

Model binding and metadata using the strongly-typed helpers looks at the declared, rather than the runtime, type of the model. I consider this a bug, but apparently the MVC team disagrees with me, as my Connect issue on this was closed as "By Design."

这篇关于想知道为什么在覆盖属性的 LabelFor 中忽略 DisplayName 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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