【问题标题】:MVC3: NullReferenceException was unhandled by user code C#MVC3:用户代码 C# 未处理 NullReferenceException
【发布时间】:2012-12-12 11:26:39
【问题描述】:

当我尝试对 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
                }
            }

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

【问题讨论】:

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


    【解决方案1】:

    要么:

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

    我猜是第二个。

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

    【讨论】:

    • 感谢您的回复!我还不清楚..在异常'sidx'包含int变量&这个变量在属性中声明为public并且它不为null时,我可以在即时窗口中检查。有什么解决办法吗?
    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多