更改日期时间迄今在MVC 3 C# [英] Change datetime to date in MVC 3 C#
问题描述
我使用MVC 3 C#,并在下拉列表中显示的日期时,我有一个问题。
日期显示为: 2011年4月21日12:00:00 AM,但我只是想给他们这样的格式: 2011年4月21日,这正是我的数据库的格式。
I'm using the MVC 3 with C# and I have a problem when displaying dates in a dropdown list. Dates are displayed as follow: 4/21/2011 12:00:00 AM, but I just want to format them this way: 4/21/2011, which is exactly the format in my database.
在数据库中的日期属性是日期,而不是日期时间。
The date attribute in the database is date and not datetime.
在模型中的代码:
[DisplayFormat(DataFormatString = "{mm/dd/yyyy}", ApplyFormatInEditMode=false)]
public DateTime? booksDate { get; set; }
控制器看起来是这样的:
the controller looks like this:
var booksDates = from dates in db.Books
orderby dates.booksDate
select new { dates.booksDate};
ViewBag.Dates = BooksDates.Distinct();
终于在我看来,代码:
and finally the code in my view:
<td>Books dates</td>
<td>@Html.DropDownList("booksDate", new SelectList(ViewBag.Dates as System.Collections.IEnumerable, "booksDate", "booksDate"), "Select a date") </td>
在那里我错了?
Where i'm wrong ?
谢谢
推荐答案
您可以尝试投入使用常规HTML下拉菜单,用手设置HTML值。我敢肯定有一个更好的方式,它可能是肮脏的,但它可以完成工作。
You could try to put to use a regular html dropdown and set the html values by hand. I'm sure there's a better way and it might be dirty, but it gets the job done.
控制器:
ViewBag.Dates = BooksDates.Distinct();
查看:
<select>
@foreach (var date in ViewBag.Dates)
{
<option>
@Convert.ToDateTime(date.StartDate).ToShortDateString()
</option>
}
</select>
这篇关于更改日期时间迄今在MVC 3 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!