DataAnnotation属性哥们类陌生感 - ASP.NET MVC [英] DataAnnotation attributes buddy class strangeness - ASP.NET MVC

查看:284
本文介绍了DataAnnotation属性哥们类陌生感 - ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于是自动通过的EntityFramework T4模板生成(没有,不能手动以任何方式修改)本POCO类:

Given this POCO class that was automatically generated by an EntityFramework T4 template (has not and can not be manually edited in any way):

public partial class Customer
{
    [Required]
    [StringLength(20, ErrorMessage = "Customer Number - Please enter no more than 20 characters.")]
    [DisplayName("Customer Number")]
    public virtual string CustomerNumber { get;set; }

    [Required]
    [StringLength(10, ErrorMessage = "ACNumber - Please enter no more than 10 characters.")]
    [DisplayName("ACNumber")]
    public virtual string ACNumber{ get;set; }
}

注意ACNumber是一个严重命名数据库字段,所以autogenerator无法生成正确的显示名称和错误消息应该是帐号。

Note that "ACNumber" is a badly named database field, so the autogenerator is unable to generate the correct display name and error message which should be "Account Number".

所以我们手工创建这个哥们类补充说,不能自动生成自定义属性:

So we manually create this buddy class to add custom attributes that could not be automatically generated:

[MetadataType(typeof(CustomerAnnotations))]
public partial class Customer { }

public class CustomerAnnotations
{
    [NumberCode] // This line does not work
    public virtual string CustomerNumber { get;set; }

    [StringLength(10, ErrorMessage = "Account Number - Please enter no more than 10 characters.")]
    [DisplayName("Account Number")]
    public virtual string ACNumber { get;set; }
}

其中[编号code]是一个简单的正则表达式基于属性,只允许数字和连字符:

Where [NumberCode] is a simple regex based attribute that allows only digits and hyphens:

[AttributeUsage(AttributeTargets.Property)]
public class NumberCodeAttribute: RegularExpressionAttribute
{
    private const string REGX = @"^[0-9-]+$"; 
    public NumberCodeAttribute() : base(REGX) { }
}

现在,当我加载页面,显示名称属性正常工作 - 它显示从好友类的显示名称而不是生成的类

NOW, when I load the page, the DisplayName attribute works correctly - it shows the display name from the buddy class not the generated class.

在StringLength属性不能正常工作 - 它显示了从生成的类(ACNumber而不是帐号)的错误信息

The StringLength attribute does not work correctly - it shows the error message from the generated class ("ACNumber" instead of "Account Number").

但[编号code]被哥们类属性甚至不会应用到账户号码属性:

BUT the [NumberCode] attribute in the buddy class does not even get applied to the AccountNumber property:

foreach (ValidationAttribute attrib in prop.Attributes.OfType<ValidationAttribute>())
{
    // This collection correctly contains all the [Required], [StringLength] attributes
    // BUT does not contain the [NumberCode] attribute
    ApplyValidation(generator, attrib);
}

为什么在 prop.Attributes.OfType&LT; ValidationAttribute&GT;()集合不包含[编号code]属性?数code继承RegularEx pressionAttribute它继承ValidationAttribute所以应该在那里。

Why does the prop.Attributes.OfType<ValidationAttribute>() collection not contain the [NumberCode] attribute? NumberCode inherits RegularExpressionAttribute which inherits ValidationAttribute so it should be there.

如果我手动移动[编号code]属性自动生成的类,那么它被包含在 prop.Attributes.OfType&LT; ValidationAttribute&GT;()集合。

If I manually move the [NumberCode] attribute to the autogenerated class, then it is included in the prop.Attributes.OfType<ValidationAttribute>() collection.

所以我不明白的是,为什么这个特殊的属性不会在哥们类,当哥们类其他属性做工作的时候工作。而为什么这个属性作品在自动生成的类,但不是在好友。任何想法?

So what I don't understand is why this particular attribute does not work in when in the buddy class, when other attributes in the buddy class do work. And why this attribute works in the autogenerated class, but not in the buddy. Any ideas?

此外,为什么显示名称得到由好友重写,当StringLength不?

Also why does DisplayName get overriden by the buddy, when StringLength does not?

推荐答案

我重新使用VS2008和MVC2您code和它为我工作得很好。

I recreated your code using VS2008 and MVC2 and it worked fine for me.

这篇关于DataAnnotation属性哥们类陌生感 - ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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