3

So I have a duration in seconds of a video and I would like to display that duration in Razor.

Currently I am using

@TimeSpan.FromSeconds(item.Duration).ToString()

However the rest of the code I am using uses

@Html.DisplayFor(modelItem => item.Description)

Is there way to get the duration (currently an int) to display a as a timespan? using the @Html.DisplayFor syntax. The item.duration is pulling form a Entity Framework model which is held as a int in the database.

John Mitchell
  • 9,653
  • 9
  • 57
  • 91

3 Answers3

13

John,

Create your own display template. To do this, follow these steps:

  1. Create a folder called DisplayTemplates under Views/Shared
  2. Under that new folder, create a partial view called TimeSpan.cshtml
  3. Now, in your view, anytime you encounter a model property that is a timespan, it will automatically be rendered by the TimeSpan DisplayTemplate.
  4. Add a new get property to your model.

Model edit (add the following):

public TimeSpan MyTimeSpanProperty
{
    get
    {
        return TimeSpan.FromSeconds(Duration);
    }
}

TimeSpan.cshtml

@model TimeSpan
@string.Format("{0}:{1}:{2}", Model.Hours, Model.Minutes, Model.Seconds)

Invoked in your view as

@Html.DisplayFor(m => m.MyTimeSpanProperty)

This will display as 3:24:16 etc..

That's all there is to it (tho it assumes that you'll pass in a property of type TimeSpan, so if possible, see if you can make that minor change to your model !! )

jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • 4
    Just to add to this. make the format line in the `TimeSpan.cshtml` to `"{0}:{1:00}:{2:00}"` to make sure you've got the double digit padding for the minutes and seconds. `1:4:7` becomes `1:04:07` – TankorSmash Nov 26 '12 at 20:30
  • The format 00:00:00 appears to be the default format "DisplayFor". To make sure you're actually affecting the display you may want to use some different characters to make sure you're not just seeing default behavior. – William T. Mallard Apr 28 '14 at 03:21
4

You can extend your model class using a partial class definition.

public partial class My_class_that_has_been_created_by_EF {
    public string Timespan {
        get { 
            return Timespan.FromSeconds(Duration).ToString(); 
        }
    }
}

Then use it in your view with

@Html.DisplayFor(modelItem => item.Timespan)
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
2

You should take advantage of DataAnnotations namespace and DisplayTemplates in ASP.NET MVC

  1. First mask your property with UIHint Attribute and set the template to VideoDuration in Models/YourVideoModel.cs

    [UIHint("VideoDuration")]
    public int Duration { get; set; }
    
  2. Add a display template to your project at Views/Shared/DisplayTemplates/VideoDuration.cshtml (you may need to add folder DisplayTemplates if it not present)

    @model int?
    @string.Format("{0:g}", TimeSpan.FromSeconds(Model))
    

Then you can use it in your view like this:

@Html.DisplayFor(m => m.Duration)
Toan
  • 321
  • 3
  • 6