I have a model, which contains a DateTime field TimeRecorded. I have also defined an extension method for DateTime, which compares it to DateTime.Now and describes approximately “how long ago” it was in informal English (not unlike the timestamps on the StackOverflow homepage).
In my view, I attempted the following:
@String.Format("{0} - {1}", Html.DisplayFor(m => m.Value),
Html.DisplayFor(m => m.TimeRecorded.ToRelativeString()))
I am greeted with an InvalidOperationException, informing me that I can only use DisplayFor with “field access, property access, single-dimension array index or single-parameter custom indexer expressions.”
Now, I know I could easily create a display template for this; however, I would rather avoid the bloat of having a whole separate file just for the sake of formatting a single value. Is there a ‘conventional’ way of doing this within my current view?
Thanks
Just do:
In fact… I don’t know what
Model.Valueis, but could you simply do: