[英]Why does ASP.NET MVC complain of a null parameter when data is posted from IE and NOT firefox?
Something odd going on here. 奇怪的事情发生在这里。
I have some JS that posts to a ASP.NET MVC ActionMethod that works find in every browser other than ANY version of IE. 我有一些JS发布到ASP.NET MVC ActionMethod,它可以在除IE的任何版本之外的每个浏览器中查找。 The code in question is as follows:
有问题的代码如下:
$.ajax({
url: path,
type: 'POST',
data: { team: team_copy[team_copy.length - 1], queryDate: d.toUTCString(), newOutlets: newOutlets },
success: function (MyResponseObject) {
holder.append(MyResponseObject.content);
//locate active section and click to show new content - its a mess, but it works
//activeMenu.click();
MessageSystem.showMessage("Target Data System", MyResponseObject.message, false);
if (team_copy.length > 1) {
team_copy.pop();
$('#actualprogress').animate({ width: '+=' + TargetReports.progressratio + '%' }, 'slow');
TargetReports.getTeamData(team_copy, d, newOutlets);
}
else {
MessageSystem.showMessage("Complete", "All Data Fetched", false);
$('#show-calendar-selection').fadeIn();
TargetReports.buildTotalsTable("daysandcalls", "daysandcallstotal");
TargetReports.buildTotalsTable("volumeanddistribution", "volumeanddistributiontotal");
TargetReports.buildTotalsTable("outletactivation", "outletactivationtotal");
TargetReports.buildTotalsTable("promotion", "promotiontotal");
//$('#progress').fadeOut().remove();
$('#results-options').fadeIn();
$('#total-holder').fadeIn();
activeMenu.click();
//update link to download file
var hidden = $('.hidden-information').first();
var newOutlets = encodeURIComponent($('input[name="newoutlets"]', hidden).val());
var queryDate = encodeURIComponent($('input[name="enddate"]', hidden).val());
var anchor = $('#get-target-reports');
var link = anchor.attr('href');
link = "/manager/TargetResults.csv?endDate=" + queryDate + "&newOutlets=" + newOutlets;
anchor.attr('href', link);
}
}
});
The Action Method signature looks like: Action Method签名如下所示:
public ActionResult GenerateTargetData(int team, DateTime queryDate, bool forceRegen = false, bool newOutlets = false)
When running in IE .NET will complain of a null entry for the queryDate parameter. 在IE .NET中运行时,会抱怨queryDate参数的空条目。 Using the debug tools in IE I can see that the request body looks as follows:
使用IE中的调试工具,我可以看到请求体看起来如下:
team=7&queryDate=Mon%2C+29+Nov+2010+23%3A15%3A39+UTC&newOutlets=false
And in firefox, which works: 在Firefox中,它有效:
team=7&queryDate=Mon%2C+29+Nov+2010+23%3A10%3A46+UTC&newOutlets=false
I really cant see whats going in here. 我真的不知道这里有什么。 All help appreciated!
所有帮助赞赏!
Your problem seems because ASP.net MVC model binder will accept a datetime in ISO8601 format. 您的问题似乎是因为ASP.net MVC模型绑定器将接受ISO8601格式的日期时间。
If the time is in UTC, add a 'Z' directly after the time without a space.
如果时间是UTC,则在没有空格的时间之后直接添加“Z”。 'Z' is the zone designator for the zero UTC offset.
'Z'是零UTC偏移的区域指示符。 "09:30 UTC" is therefore represented as "09:30Z" or "0930Z".
因此,“09:30 UTC”表示为“09:30Z”或“0930Z”。 "14:45:15 UTC" would be "14:45:15Z" or "144515Z".
“14:45:15 UTC”将是“14:45:15Z”或“144515Z”。
I have checked with chrome 12.0.733.0 dev, Firefox 4, IE 9. If you call javascript toUTCString(), different browser return different stuff. 我已经检查过chrome 12.0.733.0 dev,Firefox 4,IE 9.如果你调用javascript toUTCString(),不同的浏览器返回不同的东西。 Chrome and Firefox will return "Wed, 20 Apr 2011 20:31:11 GMT ", only IE return "Wed, 20 Apr 2011 20:31:11 UTC "
Chrome和Firefox将返回“Wed,20 Apr 2011 20:31:11 GMT ”,只有IE返回“Wed,2011年4月20日20:31:11 UTC ”
d.toUTCString().replace(' UTC','Z') will work for you.
d.toUTCString()。replace('UTC','Z')对你有用。
Use 采用
queryDate: d.toISOString()
instead of 代替
queryDate: d.toUTCString()
This formats date according to ISO standard (to something like 2012-07-09T15:44:03.114Z) and is happily accepted by ASP.NET MVC 这根据ISO标准(类似于2012-07-09T15:44:03.114Z)格式化日期,并且很高兴被ASP.NET MVC接受
Reference: http://www.w3schools.com/jsref/jsref_toisostring.asp 参考: http : //www.w3schools.com/jsref/jsref_toisostring.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.