c# - Retrieve [Display] Attribute Name in HTML Helper -
we have custom html helper following signature.
public static mvchtmlstring textboxfor<tmodel, tproperty>(this htmlhelper<tmodel> helper, expression<func<tmodel, tproperty>> property) { var htmlattributes = new dictionary<string, object>(); // .... return helper.textboxfor(property, htmlattributes); } i implement placeholder attribute here, should set label control. label can either string constant or string specified property's [display] attribute.
is there way me label helper function?
this seems work:
modelmetadata metadata = modelmetadata.fromlambdaexpression(property, helper.viewdata); string htmlfieldname = expressionhelper.getexpressiontext(property); string labeltext = metadata.displayname ?? metadata.propertyname ?? htmlfieldname.split('.').last(); if (!string.isnullorwhitespace(labeltext)) // labeltext contains label
Comments
Post a Comment