replace character in text generated by html.displayfor(function

1.8k Views Asked by At

A webpage currently gives a device ID with hyphens in the number, I want to replace - with : instead (that is to say, replace any hyphens with a colon), but only at display time. I'm pretty confident this is line that is generating the device ID shown on the page:

@Html.DisplayFor(Function(m) m.DeviceID)</span> <br />

Is it possible to change this output to include colons instead?

1

There are 1 best solutions below

1
On BEST ANSWER

You can create a display template. Create the following partial view ~/Views/Shared/DisplayTemplates/_DeviceWithColons.cshtml:

@model string

@( Model.Replace('-', ':') )

Now in your View, specify:

@Html.DisplayFor(m => m.DeviceID, "_DeviceWithColons")

You can then later reuse this display template where required.