views:

30

answers:

1

I found the follwing code in MSDN . How can I change this "UnitsInStock" with a value that my Product class has. For instance, Product has a FieldType property which has a string property called Name. I'd like to use that FieldType.Name property instead of hardcoding with a string. However, I cannot specify "this", or the current instance as an argument of the UIHint attribute . Please, help

using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(ProductMetadata))]
public partial class Product 
{

}

public partial class ProductMetadata
{
    [UIHint("UnitsInStock")]
    [Range(100, 10000, 
    ErrorMessage = "Units in stock should be between {1} and {2}.")]
    public object UnitsInStock;

}
+1  A: 

I don't believe this is possible because the attribute is applied to the class, not an instance of the class.

So at runtime, frameworks will generally be accessing the attribute through the Type and that's independant of any running instance.

I tried for a while to get a static method called as part of the constructor but couldn't even get that to work.

Sorry about the lack of help!

Mac