ASP.NET MVC - 传递JSON日期时间到控制器不映射到控制器参数 [英] ASP.NET MVC - Passing JSON DateTime to controller not mapping to controller parameters

查看:301
本文介绍了ASP.NET MVC - 传递JSON日期时间到控制器不映射到控制器参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery的日历显示事件,其目的是从服务器上获取数据。
在是吗掀起了AJAX请求日历大火得到事件对象的数组(JSON EN codeD)。
所有好为止。
然而,该请求包括一个JSON烯$ C $光盘日期和时间(在leats我的执行力度一样)。
在code是这样的:

I am using a jQuery calendar to display events, which is designed to pull data from the server. On innit the calendar fires off a AJAX request to get an array of events objects (json encoded). All good so far. However, this request includes a JSON encoded date and time (at leats my implimentation does). The code looks like this:

data: function (start, end, callback) {
        $.post('/planner/GetPlannerEvents', { test: "test", start: JSON.stringify(start), end: JSON.stringify(end) }, function (result) { callback(result); });
    }

对于GetPlannerEvents控制器方法的声明如下:

The declaration for the GetPlannerEvents controller method looks like this:

public ActionResult GetPlannerEvents(DateTime start, DateTime end)

问题是,asp.net MVC 2似乎无法自动解析JSON EN codeD datetime和这样抱怨的开始和结束值为空。

The problem is that asp.net mvc 2 cannot seem to automatically parse the json encoded datetime and as such complains that the start and end values are null.

有另一种方法,我应该使用到的JavaScript日期传递给服务器,以便它们可以被正确解析?

Is there another method I should be using to pass the javascript dates to the server so that they may be parsed correctly?

谢谢,

推荐答案

您不应该JSON编码与字符串化的日期,因为默认模型联不希望JSON。试试这个:

You shouldn't be JSON encoding the dates with stringify because the default model binder doesn't expect JSON. Try this:

$.post('/planner/GetPlannerEvents', { start: start.toUTCString(), 
    end: end.toUTCString() }, function (result) {
    callback(result); 
});

这篇关于ASP.NET MVC - 传递JSON日期时间到控制器不映射到控制器参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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