【发布时间】:2014-01-15 17:29:52
【问题描述】:
我正在格式化DateTime? 字段,如dd/MM/yyyy,当我提交表单时显示验证错误。
我不明白为什么会这样?
型号
[Display(Name = "Expected Ending Time")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? ExpectedEndingTime { get; set; }
HTML
@Html.TextBoxFor(x => x.Requsition.ExpectedEndingTime, new { @class = "form-control dataPickerField", id = "ExpectedEndingTimeDataPicker", @readonly = true })
@Html.ValidationMessageFor(x => x.Requsition.ExpectedEndingTime)
<script>
$(function () {
$('#ExpectedEndingTimeDataPicker').datepicker({
format: 'dd/mm/yyyy',
autoclose: true
})
.on('changeDate', function (ev) {
// do things;
);
});
</script>
【问题讨论】:
-
如果输入 MM/dd/yyyy 值,会起作用吗?如果是,那么不知何故您的格式不适用
-
您能将 ExpectedEndingTime 类型从 DateTime 更改为 String 并检查您从浏览器获得的值吗?
-
可能是使用可为空的值导致此问题。
-
这也可能与您的 MVC 应用程序中的区域设置有关。您是否尝试过查看
01/15/2014是否有效? -
你用什么DataPicker?
标签: c# .net asp.net-mvc validation displayformat