asp.net - Use DisplayFormat within a custom DisplayTemplate -


if you've defined custom displaytemplate, there way use displayformat.dataformatstring defined object?

for example, i've defined displaytemplate double:

<div class="form-group">     @html.label("", viewdata["label"] string, new { @class = "col-sm-6" })     <p class="col-sm-6">         @html.displaytextfor(m => m)     </p> </div> 

then model defines displayformat:

[display(name = "in weight")] [displayformat(dataformatstring = "{0} lb")] public double? inweight{ get; set; } 

in above example, displaytextfor provides simple tostring(), while replacing displayfor renders nothing.

the @html.displaytextfor(m => m) method not take account displayformatattribute , @html.displayfor(m => m) not work because recursively calling creating endless loop. need access properties modelmetadata.

note not generating form control, use of <label> element not appropriate.

your template should like

@model system.double <div>     <div>@viewdata.modelmetadata.getdisplayname()</div>     <div>@string.format(viewdata.modelmetadata.displayformatstring ?? "{0:0.00}", model)</div> </div> 

and add class names enclosing elements style it

note have added 'default' format properties [displayformat] may not applied.

side note: since property nullable, enhance further including like

@if (model == null) {     <div>@(viewdata.modelmetadata.nulldisplaytext ?? "not assigned")</div> } else {     <div>@string.format(viewdata.modelmetadata.displayformatstring ?? "{0:0.00}", model)</div> }