2

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.

2
  • 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. Commented May 16, 2015 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. Commented May 18, 2015 at 6:44

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.