从Ajax调用MVC传递日期值 [英] Pass Date Values from Ajax Call to MVC

查看:229
本文介绍了从Ajax调用MVC传递日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Ajax调用

 $('#QuickReserve').click(function () {
        var now = new Date();
        alert(now);

        var _data = {
            'ComputerName': _computerName,
            '_mStart': now.toTimeString(),
            '_mEnd': now.toDateString()
        };
        $.ajax({
            cache: false,
//            contentType: "application/json; charset=utf-8",
            type: "POST",
            async: false,
            url: "/Home/SetMeeting",
            dataType: "json",
            data: _data,
            success: "",
            error: function (xhr) {
                alert("Error");
                alert(xhr.responseText);
            }
        });
    });

我的C#code

MY C# code

 public ActionResult SetMeeting(string ComputerName, DateTime? _mStart, DateTime? _mEnd)
        {
           }

DateTime值不会在code月底收到.....他们只是显示为空白。
jQuery中当我试图

DateTime values are not received at code end..... They just appear blank. In jquery when i tried to

'_mStart': now.toTimeString(),
            '_mEnd': now.toDateString()

要datestring不会返回今天的日期,但是,我想要的日期和时间也是时间的一部分。

to datestring does return today's date, but, i want time part of date time also.

推荐答案

不要做与数据格式的任何技巧。只需使用标准函数<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString\">date.toISOString()它返回ISO8601格式。

Don't do any tricks with data formats. Just use standard function date.toISOString() which returns in ISO8601 format.

从JavaScript

from javascript

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

从C#

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

这篇关于从Ajax调用MVC传递日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆