2013-09-25 94 views
0

我想通過ajax發送一個對象到asp.net服務器。對象中有更多的datetime屬性不被識別。我的意思是參數中的日期時間是默認值=> 0001.01.01發送javascript日期到asp.net服務器

var a = {start: new Date(), title: "asdf", description: "asdfasdf", allDay: true, end: new Date()}; 

$.ajax({ 
      url : m_serverName + "/api/calendar", 
      type : "post", 
      data : a, 
      dataType: 'json', 
      success : function() { 
       console.log("ok") 
      }, 
      error : function(request, status, error) { 
       console.log(request) 
      }, 
      xhrFields : { 
       withCredentials : true 
      } 
     }); 

只有一種方法,它是如何工作的。如果我使用toJSON並將其作爲JS發送到服務器。

服務器代碼:

public HttpResponseMessage Post(CalendarEvent calendarEvent) 
     { 
      int id = -1; 

      var httpCookie = HttpContext.Current.Request.Cookies["token"]; 
      if (httpCookie != null) 
       if (!int.TryParse(httpCookie.Value, out id)) 
        return Request.CreateResponse(HttpStatusCode.OK, false); 
       else 
       { 
        int employeeId = service.GetByEmployeeDevice(id).Id; 
        return Request.CreateResponse(HttpStatusCode.OK, 
                service.GetEventsById(employeeId)); 
       } 

      return Request.CreateResponse(HttpStatusCode.OK, false); 
     } 

域模型

[JsonObject] 
    public class CalendarEvent 
    { 
     [JsonProperty("id")] 
     public int Id { get; set; } 

     [JsonProperty("employeeId")] 
     public int EmployeeId { get; set; } 

     [JsonProperty("title")] 
     public string Title { get; set; } 

     [JsonProperty("description")] 
     public string Description { get; set; } 

     [JsonProperty("location")] 
     public string Location { get; set; } 

     [JsonProperty("partner")] 
     public string Partner { get; set; } 

     [JsonProperty(PropertyName = "allDay")] 
     public bool AllDay { get; set; } 

     [JsonProperty(PropertyName = "start")] 
     public DateTime Start { get; set; } 

     [JsonProperty(PropertyName = "end")] 
     public DateTime End { get; set; } 

     [JsonProperty(PropertyName = "type")] 
     public int Type { get; set; } 

     [JsonIgnore] 
     public Employee Employee { get; set; } 

     //navigation property 
     //public ICollection<AgentDevice> Devices { get; set; } 
    } 

我如何通過AJAX發送此,沒有任何convertation,因爲如果我有somany對象的數組是很困難的。

回答

1

你可以把他們作爲ISO 8601字符串:

var a = { 
    start: (new Date()).toISOString(), 
    title: "asdf", 
    description: "asdfasdf", 
    allDay: true, 
    end: (new Date()).toISOString() 
}; 

你打在服務器上的ASP.NET Web API使用Newtonsoft JSON序列化,這將能夠正確地反序列化他們爲DateTime實例。

+0

我可以使用toJSON方法,並且服務器會識別它,但是如果我有一個包含數千個項目的數組。我是否必須轉換所有數組項目的所有日期屬性? – user1693057

+1

是的,你必須。 –

+0

noo,whyyy?我不想相信它 – user1693057

0

只需使用鍵入的列表/數組... List<CalendarEvent>CalendarEvent[]作爲您的數據類型,並且它應該適當地解碼ISO-8601編碼的日期。

+0

我明白,但在我必須將日期對象轉換爲JSON或轉換爲ISOString之前,不是嗎? – user1693057

+0

在JavaScript中,當使用JSON.stringify(...)序列化某些內容時,默認情況下將使用UTC/GMT作爲ISO-8601樣式日期時間的日期時間實例序列化。 – Tracker1

0

的祕訣是:JsonConvert.DeserializeObject(customerJSON);

public HttpResponseMessage Post([FromBody]String customerJSON) 
     { 
      if (customerJSON != null) 
      { 
       int id = -1; 

       var httpCookie = HttpContext.Current.Request.Cookies["token"]; 

       Customer customer = JsonConvert.DeserializeObject<Customer>(customerJSON); 


       if (httpCookie != null) 
        if (!int.TryParse(httpCookie.Value, out id)) 
         return Request.CreateResponse(HttpStatusCode.OK, false); 
        else 
        { 
         customer.ContactId = employeeService.GetByEmployeeDevice(id).Id; 
         return Request.CreateResponse(HttpStatusCode.OK, 
                 service.Add(customer)); 
        } 
      } 

      return Request.CreateResponse(HttpStatusCode.OK, false); 
     }