Raphael
Raphael

Reputation: 1857

Action works with POST, but not with GET (query string)

I've been using an Action for obtaining a report's data, as a JSON object, with no problems POSTing a form to it with jQuery's ajax.

But now I need to return different result types depending on a parameter's value. It should return either JSON, an Excel file (built with HTML) or a PDF file. So I have created a nested enum on my controller class for delimiting the available return types.

But now, when I try to invoke the Action from a URL for generating the files, it throws an ArgumentException with the message:

The parameters dictionary contains a null entry for parameter 'dataInicio' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult DadosRelatorioResumoLancamentos(System.Nullable`1[System.Int32], System.String, System.DateTime, System.DateTime, TipoResultado)' in 'Imovelweb.Web.Intranet.Controllers.RelatoriosController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

Still, the dataInicio parameter is present at the query string:

http://localhost:32331/Relatorios/DadosRelatorioResumoLancamentos?codFantasia=106&numAp=&dataInicio=21%2F03%2F2012&dataFim=21%2F03%2F2012&tipoResultado=1

I have tried the original request (wich returns JSON content) with both methods, and it works with POST, but not with GET (the same ArgumentException is thrown).

What am I missing?


Here is the Action method's signature:

public ActionResult DadosRelatorioResumoLancamentos(
    int? codFantasia, 
    string numAp, 
    DateTime dataInicio, 
    DateTime dataFim, 
    TipoResultado tipoResultado = TipoResultado.Json
);

And here is the enum:

public enum TipoResultado
{ 
    Json,
    Excel,
    Pdf
}

Upvotes: 2

Views: 1239

Answers (1)

pjumble
pjumble

Reputation: 16960

I had this problem, the default ASP.NET MVC model binder parses QueryString values as InvariantCulture, whereas POSTed form values will get parsed using CurrentCulture.

This means in your GET request it will attempt to parse 21/03/2012 in the American format MM/dd/yyyy, which is invalid. Since your dataInicio parameter isn't nullable it won't be able to supply a suitable value so it'll throw an ArgumentException.

There's a full write-up / workaround here: http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

Upvotes: 7

Related Questions