Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

 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. Using the debug tools in IE I can see that the request body looks as follows:

team=7&queryDate=Mon%2C+29+Nov+2010+23%3A15%3A39+UTC&newOutlets=false

And in firefox, which works:

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!

like image 673
Sergio Avatar asked Nov 29 '10 23:11

Sergio


2 Answers

Your problem seems because ASP.net MVC model binder will accept a datetime in ISO8601 format.

If the time is in UTC, add a 'Z' directly after the time without a space. 'Z' is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "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 and Firefox will return "Wed, 20 Apr 2011 20:31:11 GMT", only IE return "Wed, 20 Apr 2011 20:31:11 UTC"

d.toUTCString().replace(' UTC','Z') will work for you.

like image 174
Kuroro Avatar answered Oct 14 '22 22:10

Kuroro


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

Reference: http://www.w3schools.com/jsref/jsref_toisostring.asp

like image 33
algiecas Avatar answered Oct 14 '22 23:10

algiecas