c# - 将日期时间从 javascript 传递给 c# (Controller)

如何使用 jquery 和 mvc3 将日期时间(我需要它到第二个)传递给 c#。这就是我所拥有的

var date = new Date();    
$.ajax(
   {
       type: "POST",
       url: "/Group/Refresh",
       contentType: "application/json; charset=utf-8",
       data: "{ 'MyDate': " + date.toUTCString() + " }",
       success: function (result) {
           //do something
       },
       error: function (req, status, error) {
           //error                        
       }
   });

我不知道日期应该采用什么格式,以便 C# 理解。

最佳答案

尝试使用 toISOString()。它返回 ISO8601 格式的字符串。

GET方法

javascript

$.get('/example/doGet?date=' + new Date().toISOString(), function (result) {
    console.log(result);
});

c#

[HttpGet]
public JsonResult DoGet(DateTime date)
{
    return Json(date.ToString(), JsonRequestBehavior.AllowGet);
}

POST 方法

javascript

$.post('/example/do', { date: date.toISOString() }, function (result) {
    console.log(result);
});

c#

[HttpPost]
public JsonResult Do(DateTime date)
{
     return Json(date.ToString());
}

https://stackoverflow.com/questions/5523870/

相关文章:

javascript - 这个简单的字符串是否被认为是有效的 JSON?

c# - 在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?

python - 如何在 Python 中将数字列表转换为 jsonarray

json - 如何对 Windows Azure 动态内容启用 gzip HTTP 压缩

python - 将 JSON 数组转换为 Python 列表

java - Maven 与 npm 类似吗?

json - JSON 有哪些好的 CLI 工具?

java - Gson:有没有更简单的方法来序列化 map

javascript - 将 HTML 映射到 JSON

java - Android 上的最佳 REST 客户端框架/实用程序