1

The following is a snippet taken from my View Model:

[Display(Name = "Time")]
    [Required(ErrorMessage = "Is required field. Format hh:mm (24 hour time)")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:HH:mm}")]
    public Nullable<System.TimeSpan> EventPerformance_Time { get; set; }

And this is from my View.cshtml:

<div class="form-group">
            @Html.LabelFor(model => model.EventPerformance_Time, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EventPerformance_Time, "{0:HH:mm}", new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EventPerformance_Time, "", new { @class = "text-danger" })
            </div>
        </div>

When I run the project, I keep getting the following error:

FormatException: Input string was not in a correct format.

At line @Html.EditorFor.....etc.

I have tried so many different 'solutions' and none have worked and I am so confused as to what is wrong because in another View Model it was working perfectly!

Any help please? Much appreciated.

ekad
  • 14,436
  • 26
  • 44
  • 46
poplebop
  • 13
  • 6
  • Just off the top of my head, isn't the minute format character n (as in HH:nn) in a format string for date and time. I think m is for months only. – Pete Stensønes May 16 '15 at 21:46
  • I've been using m and it worked to represent minutes. I think to represent months it would need to be MM instead as capitals. But of course i could be wrong. – poplebop May 18 '15 at 06:44

2 Answers2

2

I would be interesting in seeing your example where this works. What Version of MVC are you using? In the EditExtension.EditorFor or EditorExtensions.EditorFor Methods documentation I don't see a method signature that supports what you are dong. Can you maybe do something along these lines Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?

Community
  • 1
  • 1
ToddB
  • 1,464
  • 1
  • 12
  • 27
  • I am using MVC5 and the code I showed is auto-generated when adding views for a controller. The only part I added myself was "{0:HH:mm}" (which I got from another solution). With regards to the other part where it is working for me, the View.cshtml is exactly as I showed above, and the only difference with the View Model is that I added extra attributes but the EventPerformance_Time part is also the same. EDIT; I also tried the format in the link you posted and it still give me the same error. – poplebop May 16 '15 at 17:17
1

this format works: "{0:hh:mm:ss}"

https://msdn.microsoft.com/en-us/library/ee372287%28v=vs.110%29.aspx

public class Test
{
    [Display(Name = "Time")]
    [Required(ErrorMessage = "Is required field. Format hh:mm (24 hour time)")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = @"{0:hh\:mm\:ss}")]
    public Nullable<System.TimeSpan> EventPerformance_Time { get; set; }
}


<div>
@Html.LabelFor(m => m.EventPerformance_Time)
<div>
    @Html.EditorFor(m => m.EventPerformance_Time)
</div>

Karthik
  • 114
  • 4