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回来了?你会使用列表还是有另一种方式?
你不能让这些功能变成静态的吗? –
基本上没有,因为这破坏了客户需要的OOP。总是我只有静态方法作为Dictionary坚持他们(否则抛出一个错误)。 –
你是说你得到一个编译错误,或运行时异常?当你试图在字典初始化非statc的Funcs? –