MVC3:NullReferenceException未被用户代码C#处理

时间:2012-12-12 11:26:39

标签: c# asp.net-mvc-3 razor jqgrid

当我尝试对JQGrid值进行排序时,我发现NullReferenceException未被用户代码异常处理。

     public ActionResult GetSearch(string sidx, string sord, int page, int rows)
            {
                if (sidx != "")
                {
//In the Session["ResourceSearch"] I have loaded the values
    DataContractClass[] SortResult = Session["ResourceSearch"] as DataContractClass[];
    //Sort the JQGrid value from input 'sidx'                
        SortResult = SortResult.OrderBy(p => p.GetType().GetProperty(sidx).GetValue(p, null)).ToArray() as DataContractClass[];
    //Load data here
                }
            }

enter image description here

只有当int属性作为sidx的输入时才会遇到问题。但是如果sidx的任何字符串属性没有问题。我能够对数据进行排序。

1 个答案:

答案 0 :(得分:2)

或者:

  • p在lambda中为空
  • sidx
  • 所代表的类型上,没有名称​​完全的公共实例属性(即案例匹配)等于p

我的猜测是第二次。

检查实际上是否存在名称等于抛出此异常时的值sidx的公共属性。顺便说一句 - 这基本上就是消息框中的第二个帮助字符串告诉你的事情。