c# - Get Metadata from navigation property -
c# - Get Metadata from navigation property -
i want know if there improve way navigation property metadata values help of modelmetadataproviders class?
example of cshtml:
@model ienumerable<webapp.web.service.officeservice.playerdeposit> <div id="pagedlist"> <table class="table study tablesorter table-bordered table"> <thead> <tr> <th class="table-head sorttable">@html.displaycolumnnamefor(model, m=> m.paymentprovider.name) </th> <th class="table-head sorttable">@html.displaycolumnnamefor(model, m=> m.country.countryname) </th> <th class="table-head sorttable">@html.displaycolumnnamefor(model, m=> m.numberofplayers) </th> <th class="table-head sorttable">@html.displaycolumnnamefor(model, m=> m.amountdeposited) </th> <th class="table-head sorttable">@html.displaycolumnnamefor(model, m=> m.collected) </th> <th class="table-head sorttable">@html.displaycolumnnamefor(model, m=> m.transactionfee) </th> <th class="table-head sorttable">balance </th> <th></th> </tr> </thead> so getting metadata names had create view helper usage of modelmetadataproviders class.
i couldn't find direct methodology access navigation property names of supplied entity name, had utilize old string manipulation (lil mess know):
public static ihtmlstring displaycolumnnamefor<tmodel, tclass, tproperty> (this htmlhelper<tmodel> helper, ienumerable<tclass> model, expression<func<tclass, tproperty>> expression) { string entity = ""; string name = ""; modelmetadata metadata = null; var expressiontext = expressionhelper.getexpressiontext(expression); name = helper.viewcontext.viewdata.templateinfo.getfullhtmlfieldname(expressiontext); if (name.contains('.')) { int indexofdot = name.indexof('.'); entity = name.substring(0, indexofdot); name = name.substring(indexofdot + 1, name.length - (indexofdot + 1)); type classtype = type.gettype("webapp.web.service.officeservice." + entity); metadata = modelmetadataproviders .current .getmetadataforproperty(() => activator.createinstance(classtype), classtype, name); } else { metadata = modelmetadataproviders.current.getmetadataforproperty( () => activator.createinstance<tclass>(), typeof(tclass), name); } homecoming new mvchtmlstring(metadata.displayname); } as can see, since don't know if there improve way straight access navigation property look firstly separated entity , name(i count 1 level deep navigation property) , help of reflection got class type access entity metadata...
i tried old hack in html instantiating playerdeposit , utilize model straight within of html @html.displaynamefor reason didn't allow me(ambigious error): @html.displaynamefor(model => model.carriers.firstordefault().name).
i think right thing here utilize modelmetadata.fromlambdaexpression. it's of import note application viewdatadictionary passed fromlambdaexpression isn't going used since don't need inspect view info values. getting display name property itself. makes relatively easy allowing create correctly typed viewdatadictionary utilize call.
public static class htmlhelperextensions { public static ihtmlstring displaycolumnnamefor<tmodel, tcollection, tproperty>( htmlhelper<tmodel> helper, ienumerable<tcollection> model, expression<func<tcollection, tproperty>> expression) { if (expression == null) { throw new argumentnullexception("expression"); } var dictionary = new viewdatadictionary<tcollection>(); var metadata = modelmetadata.fromlambdaexpression(expression, dictionary); homecoming mvchtmlstring.create(metadata.displayname); } } c#
Comments
Post a Comment