ASP.NET MVC - Passing JSON DateTime to controller not mapping to controller parameters

前端 未结 4 1269
既然无缘
既然无缘 2021-02-07 02:02

I am using a jQuery calendar to display events, which is designed to pull data from the server. On innit the calendar fires off a AJAX request to get an array of events objects

相关标签:
4条回答
  • 2021-02-07 02:24

    The variations of date.toString did not work for me until I added json headers to the post. The resulting code is as follows:

    var pstData = {
      begDate: date1.toUTCString(),
      endDate :  date2.toUTCString()
    };
    
    $.ajax({
      url:'url',
      type:'POST',
      data: JSON.stringify( pstData ),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
    })
    
    0 讨论(0)
  • 2021-02-07 02:31

    Try to use date.toISOString() to pass data to server. It returns string in ISO8601 format. Also this method can be used to format dates for using in uri.

    $.post('/planner/GetPlannerEvents', { start: start.toISOString(), 
        end: end.toISOString() }, function (result) {
        callback(result); 
    });
    

    Why toISOString is better than toUTCString?
    toUTCString converts to human readable string in the UTC time zone.
    toISOString converts to universal ISO format which allows to resolve issue with regional settings and different formats.

    0 讨论(0)
  • 2021-02-07 02:41

    You shouldn't be JSON encoding the dates with stringify because the default model binder doesn't expect JSON. Try this:

    $.post('/planner/GetPlannerEvents', { start: start.toUTCString(), 
        end: end.toUTCString() }, function (result) {
        callback(result); 
    });
    
    0 讨论(0)
  • 2021-02-07 02:43

    You need to use return type as 1JsonResult1s instead of 1ActionResult1s

    your code goes somthing like this

    public JasonResult(DateTime start, DateTime end) {
        //some logic
        return Json(); // you can pass any values within Json() with new keyword
    }
    
    0 讨论(0)
提交回复
热议问题