问题
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!
回答1:
the DisplayFormat
attribute is for use with the Html.DisplayFor()
method and not for use with Html.TextBoxFor
. I would recommend using a custom EditorTemplate
to display your date format. Or use jQuery.
回答2:
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()
回答3:
Try using [DisplayFormat(DisplayFormatString={"0:d})]
.
来源:https://stackoverflow.com/questions/6536836/displayformatattribute-not-working