Reputation: 447
In my model, the field Length is a TimeSpan with the following Data Annotation:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{h\\:mm}")]
which I used per the suggestion here. I want the resulting textbox to display the current value for Length in hours and minutes.
However, in MVC 3 this doesn't seem to work anymore, because it causes a FormatException
. This exception occurs whether or not Length has an existing value.
Visual Studio adds, "When converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object." I don't even know what that means. My variable is a TimeSpan, not a string, so why is it attempting to do a string conversion in the first place? The dialog box reporting the exception gives a dead link.
This is the code in my view:
<div class="editor-label">
@Html.LabelFor(model => model.Length)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Length)
@Html.ValidationMessageFor(model => model.Length)
</div>
The exception occurs at Html.EditorFor()
. I'm not sure what I'm missing here, does anyone have any suggestions? Thanks!
Upvotes: 3
Views: 2820
Reputation: 93434
Your format specifier needs the position identifier:
{0:h\\:mm}
Upvotes: 3