【问题标题】:unable to evaluate viewState against Null无法针对 Null 评估 viewState
【发布时间】:2012-08-16 17:59:06
【问题描述】:

我使用这个方法来填充网格视图的数据源,但是当getnewfalse 时,它不会返回任何值,只是返回一个包含单个空值的列表。

 private List<T> GetAll_T(bool getNew)
        {
            if (getNew)
                return (ViewState["T"] = Get_T()) as List<T>;
               //Get_T() returns a CustomList 
            return new List<T>
                {
                    ViewState["T"] != null ?
                    ViewState["T"] as T: 
                    (T)(ViewState["T"] = Get_T()) Collection
                };
        }

它给了我第二行的警告[当视图状态为空时]。表达式总是错误的

为什么会有警告,当它在逻辑上是正确的时候!

【问题讨论】:

    标签: asp.net c#-4.0 object-initializers


    【解决方案1】:

    从您的代码 sn-p 中不清楚 CustomerService.GetAllCustomer() 返回什么。

    你使用它既像它在函数的第(2)行返回一个列表,又像它在第(8)行返回一个对象。

    我建议这样写

    private List<CustomerMaster> GetAllCustomer(bool getNew)
    {
        if (getNew || null == ViewState["CustomerDataset"])
            ViewState["CustomerDataset"] = CustomerService.GetAllCustomer();
        return ViewState["CustomerDataset"] as List<CustomerMaster>;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2021-10-20
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多