Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove seconds from TimeSpan EditorFor

I am creating a view containing a form in ASP.NET MVC3 for a model containing time spans. I was wondering if there is a way that I can prevent the text box that is rendered from showing the seconds part? So that instead of 12:30:00 , I would get 12:30?

Here is what I have in the model and view:

//model
[Required]
[DataType(DataType.Time)]
public TimeSpan Start { get; set; }


//view
<div class="editor-field">
        @Html.EditorFor(model => model.Start)
        @Html.ValidationMessageFor(model => model.Start)
    </div>

Any advice would be much appreciated.

like image 245
Tom Haigh Avatar asked Jun 10 '11 08:06

Tom Haigh


1 Answers

You could use the [DisplayFormat] attribute:

[Required]
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm}")]
public TimeSpan Start { get; set; }
like image 83
Darin Dimitrov Avatar answered Oct 31 '22 14:10

Darin Dimitrov