ASP.NET MVC App中的日期和时间显示格式

时间:2017-05-23 04:35:37

标签: c# asp.net asp.net-mvc date

我想将日期编辑为"23/5/2017",将时间编辑为"03:53 PM"。我的模型的日期DisplayFormat"{0:dd/M/yyyy}",时间DisplayFormat"{0:hh:mm tt}"

[DataType(DataType.DateTime)]
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:dd/M/yyyy}", ApplyFormatInEditMode = true)]
public DateTime LogInDate { get; set; }

[DataType(DataType.DateTime)]
[Display(Name = "Time In")]
[DisplayFormat(DataFormatString = "{0:hh:mm tt}", ApplyFormatInEditMode = true)]
public DateTime LogInTime { get; set; }

查看代码:

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

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

以下是日期的HTML:

<input class="form-control text-box single-line input-validation-error" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="LogInDate" name="LogInDate" type="datetime" value="">

和时间的HTML:

<input class="form-control text-box single-line input-validation-error" data-val="true" data-val-date="The field Time In must be a date." data-val-required="The Time In field is required." id="LogInTime" name="LogInTime" type="datetime" value="">

当我想输入数据时,我收到此错误:enter image description here

索引页面工作正常:

[Date/Time displays correctly]

此外,如果我可以对此进行排序,是否可以使用DataFormatString中的任何格式说明符(来自Microsoft的“自定义日期和时间格式字符串”页面)来显示/编辑我需要的日期/时间?

非常感谢你帮助我。

0 个答案:

没有答案