【发布时间】:2015-05-16 16:24:51
【问题描述】:
以下是从我的视图模型中提取的 sn-p:
[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; }
这是来自我的 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>
当我运行项目时,我不断收到以下错误:
FormatException:输入字符串的格式不正确。
在@Html.EditorFor.....等行
我尝试了这么多不同的“解决方案”,但都没有奏效,我很困惑是哪里出了问题,因为在另一个视图模型中它工作得很好!
有什么帮助吗?非常感激。
【问题讨论】:
-
就在我的脑海中,日期和时间的格式字符串中的分钟格式字符 n(如 HH:nn)不是。我认为 m 只是几个月。
-
我一直在使用 m,它可以表示分钟。我认为要代表月份,它需要是 MM 而不是大写字母。但当然我可能是错的。
标签: c# asp.net-mvc exception format