再次..参数字典包含参数的空条目

时间:2016-04-30 18:10:08

标签: asp.net-mvc asp.net-mvc-4

我遇到了一个奇怪的行为..我有一个返回Excel文件的动作:

Handler/Events.hs:10:29:
    No instance for (Foldable Entity) arising from a use of ‘.=’
    In the expression: "data" .= events
    In the first argument of ‘object’, namely ‘["data" .= events]’
    In the second argument of ‘($)’, namely ‘object ["data" .= events]’

然后我提出了我的观点:

   public ActionResult AgentPortfolioReport(DateTime fromDate, DateTime toDate)
   {
        ...

        return File(ms.ToArray(), "application/vnd.ms-excel", "SomeReport.xlsx");
   }

一切正常,文件下载。然后变量日期发挥作用。我尝试使用一个带有函数的干净javascript:

 @Html.ActionLink("Get Report", "AgentPortfolioReport", new { fromDate = DateTime.Today.AddDays(-15), toDate = DateTime.Today }, new { @class = "iin-submit", id = "loadPortfolio" })

繁荣!我得到一个例外:

function LoadReport(action, fromDate, toDate) {
        window.location = '/Reports/' + action + '?fromDate=' + encodeURIComponent(fromDate) + '&toDate=' + encodeURIComponent(toDate);
    }

我也尝试用这样的表格来解决这个问题:

The parameters dictionary contains a null entry for parameter 'toDate' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult AgentPortfolioReport(System.DateTime, System.DateTime)' in 'AgentsNetwork.Controllers.ReportsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

但结果是一样的。添加可空id参数没有帮助。请给我一个提示:

1 个答案:

答案 0 :(得分:0)

默认情况下,MVC在其模型绑定器中使用不变文化。不幸的是,如果您为活页夹提供了不匹配的日期/时间格式,则会导致DateTime解析器中断。要解决此问题,您可以创建一个自定义模型绑定器,并根据需要更改culture code

using System;
using System.Globalization;
using System.Web.Mvc;

public class DateTimeBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        return value.ConvertTo(typeof(DateTime), new CultureInfo("ru-RU"));
        }
    }
}

Application_Start()中的Global.asax.cs方法中添加以下两行:

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeBinder());