DisplayFormatAttribute 不起作用
我将 MVC3 与 Razor 和 EntityFramework 一起使用。为了处理事件调度,我有一个 StartDate、StartTime、EndDate、EndTime 使用 jquery datepicker 和 timepicker。我试图在 ViewModel 中使用 DisplayFormatAttribute 但文本框不断显示整个 DateTime 字符串。
在我的视图模型中,我具有以下属性:
[DisplayFormatAttribute(DataFormatString = "{0:mm/dd/yyyy}")]
public DateTime StartDate { get; set; }
[DisplayFormatAttribute(DataFormatString = "{0:h:mm tt}")]
public DateTime StartTime { get; set; }
我的视图如下所示:
<div id="event-datetime-range" style="display:inline">
<span id="event-startdatetime">
@Html.TextBoxFor(model => model.StartDate, new { placeholder = "Start Date", id = "event-start-date", @class = "event-datepicker" });
<span class="event-timerange">at</span>
@Html.TextBoxFor(model => model.StartTime, new { placeholder = "Time", id = "event-start-time", @class = "event-timepicker event-timerange" })
</span>
谁能告诉我我做错了什么?提前致谢!
I am using MVC3 with Razor and EntityFramework. To handle event scheduling I have a StartDate, StartTime, EndDate, EndTime using jquery datepicker and timepicker. I am trying to use the DisplayFormatAttribute in my ViewModel but the Textbox keeps showing the entire DateTime string.
In my viewmodel I have the following properties:
[DisplayFormatAttribute(DataFormatString = "{0:mm/dd/yyyy}")]
public DateTime StartDate { get; set; }
[DisplayFormatAttribute(DataFormatString = "{0:h:mm tt}")]
public DateTime StartTime { get; set; }
And my view looks like this:
<div id="event-datetime-range" style="display:inline">
<span id="event-startdatetime">
@Html.TextBoxFor(model => model.StartDate, new { placeholder = "Start Date", id = "event-start-date", @class = "event-datepicker" });
<span class="event-timerange">at</span>
@Html.TextBoxFor(model => model.StartTime, new { placeholder = "Time", id = "event-start-time", @class = "event-timepicker event-timerange" })
</span>
Can anyone please tell me what I am doing wrong? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DisplayFormat
属性适用于Html.DisplayFor()
方法,不适用于Html.TextBoxFor
。我建议使用自定义EditorTemplate
来显示日期格式。或者使用 jQuery。the
DisplayFormat
attribute is for use with theHtml.DisplayFor()
method and not for use withHtml.TextBoxFor
. I would recommend using a customEditorTemplate
to display your date format. Or use jQuery.如果您使用 JQuery 日期选择器,请在创建控件时使用
dateFormat
选项。jquery datepicker
同样,在日期/时间格式中,“mm”是分钟,“MM”是月份。
您可能还想尝试使用
@Html.EditorFor()
而不是@Html.TextBoxFor()
If you are using the JQuery date picker use the
dateFormat
option when you create the control.jquery datepicker
Also in the date/time format "mm" is minutes where "MM" is months.
You might also want to try
@Html.EditorFor()
instead of@Html.TextBoxFor()
尝试使用
[DisplayFormat(DisplayFormatString={"0:d})]
。Try using
[DisplayFormat(DisplayFormatString={"0:d})]
.