传递给MVC的JSON Date参数Action始终为空 [英] JSON Date parameter passed to MVC Action is always null

查看:231
本文介绍了传递给MVC的JSON Date参数Action始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列通过jQuery Ajax传递给MVC JsonResult操作的参数。在大多数情况下,这些成功到达,但是有一个日期值完全没有到达。



我需要使用什么注意事项/格式?为了让这个日期成功,我需要采取 -

  ...其他代码... 
myStory.Deadline = new Date($('#story-deadline')。val());

$ .ajax({
url:'/ Project /'+ action [2] +'/ AddStory',
data:{Summary:myStory.Summary,Size: myStory.Size,优先级:myStory.Priority,
所有者:myStory.Owner,截止日期:myStory.Deadline},
dataType:'json',
traditional:true,
type :'POST',
...剩下的代码...

JsonResult动作:

  [HttpPost] 
public JsonResult AddStory(int projectid,Story story)
{
...一些没有DateTime对象可以使用的代码...


解决方案

Microsoft使用 / Date(utcDate)/ 格式的日期,则可以将ASP.NET MVC数据序列化/ 数据类型。尝试使用

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

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

您还可以使用 Sys.Serialization.JavaScriptSerializer MicrosoftAjax .js 序列化截止日期或任何其他日期类型。



UPDATED :可能您应该使用'\ / Date(' )\ /'而不是'\\ / Date('')\\ /'。全部取决于您将插入字符串的位置。



更新2 :现在我有了! ASP.NET MVC主要用于每个Ajax的发布Form字段。在服务器端将只使用解析每个类型的方法将发布的参数转换为类型。所以可以使用 DateTime.Parse 。例如,您可以使用像2010-08-29T13:15:00.0000000Z这样的ISO 8601格式。要在现代浏览器(firefox,chrome)中执行此操作,可以使用 toISOString()函数。为了更加独立,可以实现数据转换,如 http://williamsportwebdeveloper.com/cgi中所述/ wp /?p = 503

  var d = new Date($('#story-deadline' ).val())
// var d = new Date(); //获取日期。在这里我们只使用Now。
var dAsISOString;
if($ .isFunction(d.toISOString)){
// alert(internal toISOString 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',
// ...
});


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...

解决方案

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',
    // ...
});

这篇关于传递给MVC的JSON Date参数Action始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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