Access parent class from custom attribute

Posted by madcapnmckay on Stack Overflow See other posts from Stack Overflow or by madcapnmckay
Published on 2010-06-06T14:22:13Z Indexed on 2010/06/06 14:32 UTC
Read the original article Hit count: 295

Filed under:
|
|

Hi,

Is it possible to access a parent class from within an attribute.

For example I would like to create a DropDownListAttribute which can be applied to a property of a viewmodel class in MVC and then create a drop down list from an editor template. I am following a similar line as Kazi Manzur Rashid here.

He adds the collection of categories into viewdata and retrieves them using the key supplied to the attribute.

I would like to do something like the below,

public ExampleDropDownViewModel {

   public IEnumerable<SelectListItem> Categories {get;set;}

   [DropDownList("Categories")]
   public int CategoryID { get;set; }
}

The attribute takes the name of the property containing the collection to bind to. I can't figure out how to access a property on the parent class of the attribute. Does anyone know how to do this?

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET