2016-06-08 62 views
-1
public JsonResult GetReport(string reportSelected, string firstDateGiven) 
     { 
      _context = new ReportDB(); 

      var theResults = 
        miPolicyTransactions.Select(
         x => 
          new 
          { 
           PolicyReference = x.PolicyReference, 
           TransactionType = x.TransactionType 
           ... 
           }).ToList(); 

       var theadColumns = new[] 
       { 
        new {columnName = "Policy Reference"}, 
        new {columnName = "Transaction Code"} 
        ... 
       }.ToList(); 

       return Json(new { data = theResults, columns= theadColumns }, JsonRequestBehavior.AllowGet); 
      } 

以上是我从哪些作品开始的,但我已经使用了词典func来简化调用并创建其他内容。如何将匿名列表从静态方法传递给调用方法?

private Dictionary<string, Func<IReportDB, string, JsonResult>> functions = new Dictionary<string, Func<IReportDB, string, JsonResult>> 
       { 
        { "New Business by Agent last 3 Months(set)", NewBusinessAgentLast3Month},     

        { "New Business by Agent last 7 days (set)", SomeOtherMethodName} 
        }; 

private static JsonResult NewBusinessAgentLast3Month(IReportDB context, string parameters) 
     { 

     _context = new ReportDB(); 

     var theResults = 
       miPolicyTransactions.Select(
        x => 
         new 
         { 
          PolicyReference = x.PolicyReference, 
          TransactionType = x.TransactionType 
          ... 
          }).ToList(); 

      var theadColumns = new[] 
      { 
       new {columnName = "Policy Reference"}, 
       new {columnName = "Transaction Code"} 
       ... 
      }.ToList(); 

      return ?????????????????????????? 

我得到

一个对象引用是所必需的非静态字段,方法,属性 错误我不能返回一个JSON对象。无法在静态上下文中访问非静态Json。

我能避免为每个具体类型列表中创建一个具体类型,但还是反过来匿名列表来调用方法传递给被作为在我的Jquery使用的文件一个JsonResult回来了?你会使用列表还是有另一种方式?

+0

你不能让这些功能变成静态的吗? –

+0

基本上没有,因为这破坏了客户需要的OOP。总是我只有静态方法作为Dictionary 坚持他们(否则抛出一个错误)。 –

+0

你是说你得到一个编译错误,或运行时异常?当你试图在字典初始化非statc的Funcs? –

回答

1

你应该改变你的功能(如NewBusinessAgentLast3Month)返回object。您应该将此值传递给Controller.Json方法,该方法将创建一个JsonResult,您可以从控制器返回。

代码中的问号应该用您在重构之前使用的匿名类型替换。

+0

我走动了,这实质上是@Martins的建议。非常感谢你。 –

相关问题