【发布时间】:2017-06-18 12:37:13
【问题描述】:
我正在尝试将 DataFormatString 的格式从 dd/MM/yyyy 更改为 MM/yy。原因是因为我希望用户选择他们的信用卡到期日期。我将 HTML 分成两部分:模型和视图。
Order.cs
[Display(Name = "Expiration Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime Experation { get; set; }
地址和付款.cshtml
<div class="form-group">
@Html.LabelFor(model => model.Experation, htmlAttributes: new { @class = "col-lg-2 control-label" })
<div class="col-lg-10">
@Html.EditorFor(model => model.Experation)
@Html.ValidationMessageFor(model => model.Experation, "", new { @class = "text-danger" })
</div>
以上代码是我尝试在我的模型中将其转换为 MM/yy。但是,结果仍然是 dd/MM/yyyy。这里有我不知道的错误吗?
【问题讨论】:
-
删除
[DataType(DataType.Date)],它添加了type-"date",如果支持,它将生成一个浏览器HTML5日期选择器(但它只在Chrome和Edge中支持,所以你必须使用其中之一)。 -
但是你的格式字符串没有意义。该属性是 typeof
DateTime必须包含一个 day 组件,并且当您提交时,ModelState将无效,Experation的值将是01/01/0001(如果您启用了客户端验证,则不会甚至可以提交表格)
标签: asp.net-mvc date asp.net-mvc-4 datepicker