2013-09-25 133 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); 
     }