2012-03-21 83 views
2

我一直在使用Action來獲取報表數據,作爲JSON對象,沒有任何問題使用jQuery的ajax向其發佈表單。動作適用於POST,但不適用於GET(查詢字符串)

但現在我需要根據參數的值返回不同的結果類型。它應該返回JSON,Excel文件(用HTML構建)或PDF文件。所以我在我的控制器類上創建了一個嵌套枚舉來定義可用的返回類型。

但是現在,當我嘗試調用從URL操作產生的文件,它拋出與消息的ArgumentException

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

儘管如此,dataInicio參數出現在查詢字符串:

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

我已經嘗試了原始請求(至極返回JSON內容)這兩種方法,並將其與POST的作品名稱,但n與GET一起(拋出相同的ArgumentException)。

我錯過了什麼?


下面是操作方法的簽名:

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

這裏是枚舉:

public enum TipoResultado 
{ 
    Json, 
    Excel, 
    Pdf 
} 
+0

如何路由表的樣子嗎?在我看來,MVC正在調用不正確的操作方法 - 沒有dataInicio參數的方法? – 2012-03-21 20:25:28

+0

@TomasVoracek正確的行動正在被調用。問題在於日期格式,正如pjumble的回答中所述。 – Raphael 2012-03-22 14:04:46

回答

7

我有這個問題,默認的ASP.NET MVC模型綁定解析QueryString值爲InvariantCulture,而發佈的表單值將使用CurrentCulture解析。

這意味着在你的GET請求將嘗試在美國格式mm/dd/yyyy的,這是無效的解析21/03/2012。由於您的dataInicio參數不可爲空,因此無法提供合適的值,因此會拋出ArgumentException

有一個完整的寫了/解決方法在這裏:http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

相關問題