If I add [RequiredIf(... to a property I have on a view model class, MVC adds certain data-required... attributes to the resulting mark-up for that property.
I want to do the same; I want to decorate a property of my view model class with [MyMarkup] and have it render data-my-attribute on the rendered HTML for that property's editor.
I have looked high and low for a solution, and haven't found one, which leads me to suspect my approach may be wrong.
What I've Tried
I've tried extending Attribute but can't see how I'd access mark-up in there, since attributes wouldn't generally be concerned with mark-up.
I also looked into data annotations and metadata-aware implementations too, but can't find anything that would allow me to add a data attribute to an element.
Is this possible at all?
Please Note
I'm aware of HTML helpers and that I can pass in custom attributes to them, but I want to keep this as an attribute.
IMetadataAwareand add a value toModelMetadata.AdditionalPropertiesand then create you ownHtmlHelperextension method to generate the html. But if this is related to your last (nor deleted) question then I think your on the wrong track.DisplayIfRequiredattribute and it adddata-display-if-requiredto my HTML element. I could pass this in to the helper and be done with it, but I'd prefer to keep all this in one place, as the data annotations are. Again, much appreciated, thanks for your reply.