我有一个名为Calendar的模型,它有两个字段,start和end。这些字段都被写入DateTime变量。
模型:
public class Calendar
{
public int ID { get; set; }
[Required]
[Display(Name = "Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime start { get; set; }
[Required]
[Display(Name = "End Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime end { get; set; }
[Required]
[Display(Name = "Event")]
public string title { get; set; }
[Required]
[Display(Name = "Type")]
public string type { get; set; }
[Required]
[Display(Name = "Content")]
public string content { get; set; }
}
我已经实现了jQuery插件FullCalendar和我试图在事件处理加载我的事件。但是我注意到在文档中Fullcalendar的开始和结束格式必须符合ISO8601日期字符串标准。我试图将DateTime格式化为字符串,但我正面临着一个DateTime模型的挑战。
这是我的控制器:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult CalendarData()
{
var eventList = (from e in db.Calendars
select new
{
ID = e.ID,
title = e.title,
start = e.start,
end = e.end
}).AsEnumerable().Select(x => new webby.Models.Calendar { ID = x.ID, title = x.title, start = x.start, end = x.end }).ToList();
return Json(eventList, JsonRequestBehavior.AllowGet);
}
我想保持字段在我的模型是DateTime,否则我将不得不重组我的很多项目。这可能吗?
这是我传递的JSON结果。
[{"ID":1,"start":"/Date(1407643200000)/","end":"/Date(1407729600000)/","title":"First Title","type":null,"content":null},
{"ID":2,"start":"/Date(1407816000000)/","end":"/Date(1408161600000)/","title":"Second Title","type":null,"content":null}]
所以基本上我试图将数据转换为ISO8601。但似乎将datetime转换为这种格式的唯一方法是将其格式化为字符串,这会给我一个错误,因为我的模型是datetime。
将start = x.start改为start = x.start. tostring ("yyyyMMdd"), end