重组模型

时间:2014-08-11 14:44:47

标签: c# jquery datetime fullcalendar

我有一个名为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。

1 个答案:

答案 0 :(得分:0)

将start = x.start更改为start = x.start.ToString(“yyyyMMdd”)并将结束视为