c# - How can I extend DataAnnotationsModelMetadata -
i have custom dataannotationsmodelmetadataprovider
, i'd return object derived modelmetadata
can have properties in razor templates.
so far custom provider overrides createmetadata
function:
protected override modelmetadata createmetadata(ienumerable<attribute> attributes, type containertype, func<object> modelaccessor, type modeltype, string propertyname) { var modelmetadata = base.createmetadata(attributes, containertype, modelaccessor, modeltype, propertyname); modelmetadataattribute mma; foreach (attribute in attributes) { mma = modelmetadataattribute; if (mma != null) mma.process(modelmetadata); } return modelmetadata; }
so every attribute derived modelmetadataattribute
can custom actions (actually adds additionalvalues
)
but since of attributes add attributes html elements generate in razor template, i'd modelmetadata
in view contain dictionnary of attributes want add (and other things).
so need class inherits dataannotationsmodelmetadata
.
i can't call base.createmetadata
function since won't cast derived class.
i thought making copy of public properties of dataannotationsmodelmetadata
returned base.createmetadata
function derived class, can loose information doesn't seem safe.
another way thought of copy/paste base code of createmetadata
, add logic, seems ugly... (and have mvc3 sources, might have changed in mvc4)
also thought of inheriting viewdatadictionnary
can provide custom metadata class instead of standard one, don't have clue how this. (also admit didn't dig on particular question)
i looked @ lot of articles dataannotations
, providers couldn't find similar i'm trying do.
so options here? in direction search closer want do?
edit:
i looked @ question (quite similar): can achieve 'copy constructor' in c# copies derived class? copy of properties , wanted avoid that. in last answer in post there's populatemetadata
can't find function in base provider...
i suggest take @ mvcextensions
: http://mvcextensions.github.io/
one of it's main part doing - extensive model metadata configuration/ usage. may find lot of answers there or take "ready use" solution.
Comments
Post a Comment