English 中文(简体)
数据发布 元数据 类型
原标题:DataAnnotations MetadataType not working

I m not being able to attach a MetadataType to a auto generated class in our application. I tested setting the Order attribute in the generated class and it works fine, but if try to use another class i cannot get the attributes later.

我还尝试了建议的解决办法:here,但没有成功。

Generated class

[Table(Name = "T_MKT_Product")]
public partial class T_MKT_Product : GlobalSist.DataAccess.Base.BaseEntity
{
    [Column(Storage = "_code", DbType = "varchar(20)", IsUnique = true)]
    public virtual string Code
    {
        get { return _code; }
        set
        {
            if (_code != value)
            {
                OnPropertyChanging("Code");
                _code = value;
                OnPropertyChanged("Code");
            }
        }
    }

    [Column(Storage = "_name", DbType = "varchar(200)")]
    public virtual string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                OnPropertyChanging("Name");
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    [Column(Storage = "_description", DbType = "varchar(200)", CanBeNull = true)]
    public virtual string Description
    {
        get { return _description; }
        set
        {
            if (_description != value)
            {
                OnPropertyChanging("Description");
                _description = value;
                OnPropertyChanged("Description");
            }
        }
    }
}

然后一界定了以下课程:

[MetadataType(typeof(ProductMetaData))]
public partial class T_MKT_Product
{
}

public class ProductMetaData
{
    [Display(Order = -1)]
    public virtual string Code { get; set; }

    [Display(Order = -2)]
    public object Name { get; set; }

    [Display(Order = -3)]
    public object Description { get; set; }
}

帮助!

最佳回答

Attribute.IsDefined(currentProp, typeof(DisplayAttribute))is still False IT MUST BE FALSE. Probably you are checking the Order with a custom code! The answer to your problem is very easy: your custom code is simply WRONG. Probably it is wrong because you pretend to find the attribute added with the methadata class together with all other "native" attributes of the class. THIS IS WRONG! .Net Clr HAS NO NATIVE SUPPORT FOR MetaDataType Attribute! IT IS JUST A CONVENTION. It is up to you, verifyng that your class has a MetaDataType and retrieving also the attributes of the MetaDataType with the same name of the properties of your original class. I MEAN YOU HAVE TO DO THIS JOB MANUALLY. All Attributes that the Mvc engine handles automatically are handled this way...that is the Mvc Engine look at the attributes of the MetaDataType and merge them with the native attributes...You have to do the same in your custom code.

她说,如果你需要你的属性,就是为了......而不是人工检索你的属性,写的是“MetaDataProvider”。 元数据提供者的逻辑自动检索到所有特性(我解释的方式)。

问题回答

确保确定您的汽车类别所用的名称空间与你界定其习惯部分类别的名称相同。 例如:

namespace FooBar
{
    [Table(Name = "T_MKT_Product")]
    public partial class T_MKT_Product : GlobalSist.DataAccess.Base.BaseEntity
    {
        ...
    }
}

阁下:

namespace FooBar
{
    [MetadataType(typeof(ProductMetaData))]
    public partial class T_MKT_Product
    {
    }

    public class ProductMetaData
    {
        [Display(Order = -1)]
        public virtual string Code { get; set; }

        [Display(Order = -2)]
        public object Name { get; set; }

        [Display(Order = -3)]
        public object Description { get; set; }
    }
}




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签