0

我的代码:

public class MyCustomGridActionResultFactory : IGridActionResultFactory
{
    public System.Web.Mvc.ActionResult Create(object model)
    {
        ResultCache.AdvanceSearchGridResult = model as List<Models.Search.SearchGridModel>;

        //return a custom JSON result which have json serializer set to int.MaxValue
        return new CustomJsonResult
        {
            Data = model
        };
    }
}

结果缓存代码:

public static class ResultCache
{
    public static List<Models.Search.SearchGridModel> AdvanceSearchGridResult
    {
        get { return System.Web.HttpContext.Current.Session["AdvanceSearchGridResult"] as List<Models.Search.SearchGridModel>; }
        set { System.Web.HttpContext.Current.Session["AdvanceSearchGridResult"] = value; }
    }
}

**

模型作为List<Models.Search.SearchGridModel>返回 NULL 值

注意

ResultCache.AdvanceSearchGridResult = model;给出错误消息“无法从对象转换为列表”

**。如何解决这个问题。谢谢。

4

1 回答 1

0

什么是模型的运行时类型?'as' 返回 null 有两个可能的原因。左侧为空或不是右侧的类型。

因此,您要么将 null 传递给 Create ,要么传递给另一种类型的对象。调试以找出哪个

编辑

从评论来看,这似乎是选项二。如果发布的运行时信息是模型的类型,则它不是列表。尝试将 'List<...>' 更改为 'IEnumerable<...>'

public static IEnumerable<Models.Search.SearchGridModel> AdvanceSearchGridResult {
        get { return (IEnumerable<Models.Search.SearchGridModel>)HttpContext.Current.Session["AdvanceSearchGridResult"]; }
        set { HttpContext.Current.Session["AdvanceSearchGridResult"] = value; }
}
于 2012-10-25T06:11:55.780 回答