我使用下面的代码在剃须刀视图中弹出date的日历
模型
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[DataType(DataType.Date)]
public Nullable<System.DateTime> EndTime { get; set; }视图
<div class="form-group">
@Html.LabelFor(model => model.EndTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-4">
@Html.EditorFor(model => model.EndTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndTime, "", new { @class = "text-danger" })
</div>
</div>现在我想使用DateTime而不是Date.What应该是DataFormat字符串吗?
我的尝试
Display(Name = "End Date Time")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}{1:HH/mm}")]
[DataType(DataType.DateTime)]我得到了格式异常?
发布于 2017-04-13 11:46:09
您的EditorFor()方法使用type="date"呈现输入,因为[DataType(DataType.Date)]属性将生成浏览器HTML-5数据报头(但这仅在Chrome和边缘中得到支持)。
要生成HTML-5日期选择器,您需要使用type = "datetime-local"呈现一个输入,但是没有数据注释属性。相反,您需要自己生成type属性。
@Html.TextBoxFor(m => m.EndTime, "{0:s}", new { @type = "datetime-local", @class = "form-control" })注"{0:s}"是"{0:yyyy-MM-ddTHH:mm:ss}"的快捷方式,它将在浏览器区域性中显示日期和时间。还请注意,您不需要[DataType]属性或[DisplayFormat]属性中的ApplyFormatInEditMode = true属性,因为它们只适用于EditorFor()。
https://stackoverflow.com/questions/43390383
复制相似问题