小编Las*_*ntz的帖子

想知道为什么在重写的属性上的LabelFor中忽略DisplayName属性

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

事实证明,当我将属性放在重写的属性上时,LabelFor似乎没有注意到它.
但是,该[Required]属性在重写的属性上工作正常,并且生成的errormessage实际上使用DisplayNameAttribute.

这是一些简单的示例代码,更现实的情况是我有一个与viewmodel分开的databasemodel,但为了方便起见,我想继承databasemodel,添加View-only属性并使用UI的属性装饰viewmodel .

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; }
} …
Run Code Online (Sandbox Code Playgroud)

validation attributes asp.net-mvc-2

19
推荐指数
2
解决办法
9731
查看次数

标签 统计

asp.net-mvc-2 ×1

attributes ×1

validation ×1