编辑模式下未显示特定日期格式字符串

时间:2015-10-23 09:51:02

标签: asp.net-mvc date razor asp.net-mvc-5

我有以下显示属性

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime? StartDate { get; set; }

这在我的索引页面和我的编辑页面中正确呈现

Index.cshtml(项目清单)

 @Html.DisplayFor(modelItem => item.StartDate)

Edit.cshtml

<div class="form-group">
    @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
    </div>
</div>

但是日期格式必须采用dd / MMM / yyyy格式,例如20月/ 10月/ 2015年, 所以我将display属性更改为

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
public DateTime? StartDate { get; set; }

在我的Index.cshtml中正确显示为2015年10月20日。

问题是当我在Edit.cshtml中打开它时,它不会加载传递给ViewModel的属性值,而是显示值为

enter image description here

  1. 为什么{0:yyyy-MM-dd}工作但不是“{0:dd / MMM / yyyy}?”

  2. 如何让它发挥作用?

0 个答案:

没有答案
相关问题