4

我正在使用一些日期参数对 Iframe url 进行编码:

var object = {
                profileID: self.profileID(),
                organisationID: self.organisationID(),
                startDate: self.startDate(), // 09/04/2013 
                endDate: self.endDate() // 17/04/2013 
            };
iFrame.src = "ExportReportAllMediaDetailsCsv/?" + $.param(object);

编码的网址:

http://dev.neptune.local/Report/ExportReportAllMediaDetailsCsv/?profileID=41&organisationID=2252&startDate=09%2F04%2F2013&endDate=17%2F04%2F2013

但是,有时调用的方法无法识别传入的日期时间:

The parameters dictionary contains a null entry for parameter 'endDate' of non-nullable type 'System.DateTime'

这是方法签名:

[CustomAuthorize(Definitions.RoleAnalystManager, Definitions.RoleProjectManager)]
public ActionResult ExportReportAllMediaDetailsCsv(int profileID, int organisationID, DateTime startDate, DateTime endDate)
4

3 回答 3

1

您正在使用英国格式的日期字符串。由于 onlyendDate不起作用,我的猜测是startDate被认为是 9 月 4 日。另一方面,因为一年中没有第 17 个月,endDate所以不能绑定到一个DateTime对象。

听起来您需要正确设置文化。尝试在您的 Web.Config 中添加以下内容<system.web>

<globalization requestEncoding="utf-8" responseEncoding="utf-8"
    culture="en-GB" />

有关全球化的更多信息,请参阅http://msdn.microsoft.com/en-us/library/c6zyy3s9(v=vs.100).aspx

于 2013-04-29T07:45:41.690 回答
1

为了解决这个问题,我将日期转换为 UTC 日期时间字符串:

var object = {
                profileID: self.profileID(),
                organisationID: self.organisationID(),
                startDate: getUtcDateString(self.startDate()),
                endDate:getUtcDateString(self.endDate())
            };

function getUtcDateString(gbDate) {
    var dayMonthYear = gbDate.split("/"),
        newDate = new Date(dayMonthYear[2], dayMonthYear[1]-1, dayMonthYear[0]);
    return newDate.toUTCString();
}
于 2013-04-29T08:43:23.100 回答
0

看起来endDate有时为 null(或者根本没有设置),因此您必须将参数声明为可空类型(DateTime?):

public ActionResult ExportReportAllMediaDetailsCsv
    (int profileID, int organisationID, DateTime startDate, DateTime? endDate)

另一方面可能是因为无法识别日期时间格式,所以基本上无法解析为有效值DateTime

于 2013-04-29T07:44:14.057 回答