Javascript ASP.NET MVC - 将 JSON DateTime 传递给控制器​​而不映射到控制器参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3933245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:42:19  来源:igfitidea点击:

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

javascriptasp.net-mvc

提问by Sergio

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 (json encoded). All good so far. However, this request includes a JSON encoded date and time (at leats my implimentation does). The code looks like this:

我正在使用 jQuery 日历来显示事件,该日历旨在从服务器中提取数据。在初始化时,日历会触发 AJAX 请求以获取事件对象数组(json 编码)。到目前为止一切都很好。但是,此请求包含一个 JSON 编码的日期和时间(至少我的实现是这样)。代码如下所示:

data: function (start, end, callback) {
        $.post('/planner/GetPlannerEvents', { test: "test", start: JSON.stringify(start), end: JSON.stringify(end) }, function (result) { callback(result); });
    }

The declaration for the GetPlannerEvents controller method looks like this:

GetPlannerEvents 控制器方法的声明如下所示:

public ActionResult GetPlannerEvents(DateTime start, DateTime end)

The problem is that asp.net mvc 2 cannot seem to automatically parse the json encoded datetime and as such complains that the start and end values are null.

问题是 asp.net mvc 2 似乎无法自动解析 json 编码的日期时间,因此抱怨开始和结束值为空。

Is there another method I should be using to pass the javascript dates to the server so that they may be parsed correctly?

我应该使用另一种方法将javascript日期传递给服务器,以便正确解析它们吗?

Thanks,

谢谢,

回答by Darin Dimitrov

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

您不应该使用 JSON 编码日期,stringify因为默认模型绑定器不希望使用 JSON。尝试这个:

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

回答by rnofenko

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.

尝试使用date.toISOString()将数据传递给服务器。它以 ISO8601 格式返回字符串。此外,此方法可用于格式化在 uri 中使用的日期。

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

Why toISOStringis better than toUTCString?
toUTCStringconverts to human readable stringin the UTC time zone.
toISOStringconverts to universal ISO formatwhich allows to resolve issue with regional settings and different formats.

为什么toISOString比 好toUTCString
toUTCString转换为UTC 时区中的人类可读字符串
toISOString转换为通用 ISO 格式,可以解决区域设置和不同格式的问题。

回答by civilator

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

date.toString我将 json 标题添加到帖子之前,的变体对我不起作用。结果代码如下:

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",
})

回答by Sameer Joshi

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

您需要使用返回类型作为 1JsonResult1s 而不是 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
}