我只想在MVC应用程序中返回日期,遇到了一个问题。我已经申请了我的模型DataFormatString = "{0:dd/M/yy}"
,并按照这些帖子中的建议包括了ApplyFormatInEditMode = true
( ASP.NET MVC displaying date without time和Displaying Date only MVC )。
不过,我正在生成字段,将接下来5周的开始日期包括在下拉列表http://prntscr.com/ak825u中,如下所示。
这是通过以下代码完成的
// GET: TimeSheets/Create
public ActionResult Create()
{
int weekCount = 5;
List<DateTime> listDates = new List<DateTime>();
for (int i = 0; i < (weekCount * 7); ++i) //Get next 5 weeks
{
//Adds only next 5 mondays to the list of dates
if (DateTime.Today.AddDays(i).DayOfWeek == DayOfWeek.Monday)
listDates.Add(DateTime.Today.AddDays(i));
}
ViewData["DateList"] = new SelectList(listDates);
return View();
}
我怎样才能抽出时间只显示日期呢?
发布于 2016-03-26 11:49:54
您需要在text
和value
中设置SelectList
。
ViewData["DateList"] = new SelectList(listDates.Select(c => new SelectListItem() { Text = c.ToString("dd/mm/yy"), Value = c }));
视图中的:
@Html.DropDownListFor(m => m.SelectedDate, (SelectList)ViewData["DateList"], "Select a date")
SelectedDate
是模型中的DateTime属性。
发布于 2016-03-26 11:50:33
如果您想解析日期,这将是另一种选择。将该属性放在模型上是行不通的,因为您没有使用模型来显示日期,而是使用视图数据来显示日期。
在添加日期之后,在if条件下添加以下内容: listDatesi = ParseDate(DateTime.Today.ToString(),"MM/dd/yy");
在Display only Date in @Html.DisplayFor() in MVC的某个线程中添加顶部的应答助手类
https://stackoverflow.com/questions/36234714
复制相似问题