Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Date Values from Ajax Call to MVC

MY Ajax call

 $('#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);
            }
        });
    });

MY C# code

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

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

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

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

like image 880
Nanu Avatar asked May 20 '11 19:05

Nanu


3 Answers

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

from javascript

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

from c#

[HttpPost]
public JsonResult Do(DateTime date)
{
     return Json(date.ToString());
}
like image 155
rnofenko Avatar answered Nov 20 '22 19:11

rnofenko


Converting the json date to this format "mm/dd/yyyy HH:MM:ss" is the whole trick dateFormat is a function in jsondate format.js file found at http://blog.stevenlevithan.com/archives/date-time-format

var _meetStartTime = dateFormat(now, "mm/dd/yyyy HH:MM:ss");
like image 12
Nanu Avatar answered Nov 20 '22 19:11

Nanu


Can you not just pass one DateTime up, and separate the Date part from the time part on the server?

Can you not just pass '_mDate': now;

public ActionResult SetMeeting(string ComputerName, DateTime? _mDate)
{
   // Then in here use _mDate.Date, and _mDate.Time    
}
like image 1
ETFairfax Avatar answered Nov 20 '22 19:11

ETFairfax