Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

jquery - JSON Date parameter passed to MVC Action is always null

I have a range of parameters that are passed via jQuery Ajax to an MVC JsonResult action. For the most part, these arrive successfully, but there is a Date value that is not arriving at all.

What considerations / formats do I need to use - or what approaches do I need to take - in order to get this date to arrive successfully?

...other code ...
myStory.Deadline = new Date($('#story-deadline').val());

$.ajax({
    url: '/Project/' + action[2] + '/AddStory',
    data: { Summary: myStory.Summary, Size: myStory.Size, Priority: myStory.Priority,
            Owner: myStory.Owner, Deadline: myStory.Deadline },
    dataType: 'json',
    traditional: true,
    type: 'POST',
...the rest of the code...

The JsonResult action:

[HttpPost]
public JsonResult AddStory(int projectid, Story story)
{
...some code that doesn't have a DateTime object to work with...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Microsoft use JavaScriptSerializer to serialize/desirealize the ASP.NET MVC data. If use /Date(utcDate)/ format for the Date data type. Try to use

'"\/Date(' + myStory.Deadline.getTime() + ')\/"'

or

var d = myStory.Deadline;
var dateForMS = '"\/Date(' +
        Date.UTC (d.getUTCFullYear(), d.getUTCMonth(),
                  d.getUTCDate(), d.getUTCHours(),
                  d.getUTCMinutes(), d.getUTCSeconds(),
                  d.getUTCMilliseconds()) + ')\/"'

You can also just use Sys.Serialization.JavaScriptSerializer from MicrosoftAjax.js to serialize Deadline or any other Date type.

UPDATED: Probably you should use '/Date(' and ')/' instead of '"\/Date(' and ')\/"'. All depends on where you will insert the string.

UPDATED 2: Now I have it! ASP.NET MVC is used mostly for the posting Form fields per Ajax. On the server side will be just used Parse method for every type to convert the posted parameter to the type. So one can use any string format which are supported by DateTime.Parse. For example you can use ISO 8601 format like '2010-08-29T13:15:00.0000000Z'. To do this in modern browsers (firefox, chrome) one can use toISOString() function. To be more independend one can implement data conversion like described in http://williamsportwebdeveloper.com/cgi/wp/?p=503:

var d = new Date($('#story-deadline').val())
//var d = new Date(); // get the date. Here we use just Now.
var dAsISOString;
if ($.isFunction(d.toISOString)) {
    //alert("internal toISOString are used!");
    dAsISOString = d.toISOString();
}
else {
    dAsISOString = d.getUTCFullYear() + '-' + padzero(d.getUTCMonth() + 1) + '-' +
                   padzero(d.getUTCDate()) + 'T' + padzero(d.getUTCHours()) + ':' +
                   padzero(d.getUTCMinutes()) + ':' + padzero(d.getUTCSeconds())+'.'+
                   pad2zeros(d.getUTCMilliseconds()) + 'Z';
}
var myStory = { Summary: 'Test description', Size: 8, Dedline: dAsISOString };
$.ajax({
    url: '/Project/1/AddStory',
    data: { Summary: myStory.Summary, Size: myStory.Size, Dedline: myStory.Dedline },
    dataType: 'json',
    // ...
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...