My project is MVC5, using EF 6.1; I generate dropdownlist using the following:
Model:
public partial class LogBook
{
public string ID { get; set; }
public string TheName { get; set; }
public System.DateTime Day { get; set; }
}
Controller:
var list = db.LogBook.Where(i => i.TheName == xxx).OrderByDescending(x => x.Day).ToList();
ViewBag.Logbooks = new SelectList(list, "ID", "Day");
View:
@Html.DropDownList("logbookdate", ViewBag.LogBooks as SelectList)
The result shows:
11/11/2014 12:00 AM
How can I just display the date?
Try this:
var list = db.LogBook.Where(i => i.TheName == xxx).Select(d=> new {ID= d.TheName, Day = d.Day.ToShortDateString()).OrderByDescending(x => x.Day).ToList();
ViewBag.Logbooks = new SelectList(list, "ID", "Day");
Haven't done practical but should work.
Edit:
IEnumerable<SelectListItem> selectList =
from c in db.LogBook
where c.TheName equals xxx
select new SelectListItem
{
Text = c.ID,
Value = c.Day.ToShortDateString()
};
Then bind it to dropdonwlist on front.
You can give it a try. I have not checked syntax. Hope its fine.
Try this:
var list = db.LogBook.Where(i => i.TheName == xxx).OrderByDescending(x => x.Day).ToList();
Change in query
IEnumerable<SelectListItem> selectList = db.LogBook
.Where(i => i.TheName == xxx)
.Select(c => new SelectListItem
{
Text = c.ID,
Value = c.Day.ToShortDateString()
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.