如何在视图中显示月份名称而不是月份数字?
操作方法:
public ActionResult MyPerformance()
{
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31).AddDays(1).AddSeconds(-1);
var result = db.Chats
.Where(c => System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) >= firstDay &&
System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) <= lastDay)
.Select(x => new { x.MSTChatCreatedDateTime })
.ToList()
.GroupBy(c => new
{
Year = c.MSTChatCreatedDateTime.ToCleanDateTime().Year,
Month = c.MSTChatCreatedDateTime.ToCleanDateTime().Month
})
.Select(c => new ReportVM
{
Title = string.Format("{0}", c.Key.Month), //chart x Axis value.
ChatCountCreatdDate = c.Count() //chart y Axis value.
})
.ToList();
return View(result);
}
在视图中反映的数据是正确的,但我无法在视图中呈现月份名称。
请参阅随附的显示月份数字的图片。
谢谢!
答案 0 :(得分:0)
您可以在“MMM”中格式化显示名称,这是您的代码
public ActionResult MyPerformance()
{
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31).AddDays(1).AddSeconds(-1);
var result = db.Chats
.Where(c => System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) >= firstDay &&
System.Data.Entity.DbFunctions.TruncateTime(c.MSTChatCreatedDateTime) <= lastDay)
.Select(x => new { x.MSTChatCreatedDateTime })
.ToList()
.GroupBy(c => new
{
Year = c.MSTChatCreatedDateTime.ToCleanDateTime().Year,
Month = c.MSTChatCreatedDateTime.ToCleanDateTime().Month
})
.Select(c => new ReportVM
{
Title = c.MSTChatCreatedDateTime.ToCleanDateTime().ToString("MMMM"), //chart x Axis value.
ChatCountCreatdDate = c.Count() //chart y Axis value.
})
.ToList();
return View(result);
}