0

您好我正在将数据表传递给方法“AsJqGridResult”,但由于“System.Reflection.PropertyInfo propertyInfo”返回 NULL,“GetPropertyValue”方法返回错误。

是否有任何问题传递数据表或任何其他问题,请帮助....

控制器

[HttpPost]
    public ActionResult DynamicGridData(string sidx, string sord, int page, int rows)
    {
        var context = GetTable().AsEnumerable();
        return (context.AsQueryable().AsJqGridResult(sidx, sord, page, rows));
    }


    public DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Number", typeof(int));
        table.Columns.Add("Abr", typeof(string));
        table.Columns.Add("Country", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        table.Rows.Add(1, "AF", "AFGHANISTAN", DateTime.Now);
        table.Rows.Add(2, "AX", "ÅLAND ISLANDS", DateTime.Now);

        return table;
    }

辅助方法

public static JsonResult AsJqGridResult<T>(this IQueryable<T> source, string column, string sortOrder, int page, int pageSize)
{
  int pageIndex = Convert.ToInt32(page) - 1;
  int totalRecords = source.Count();
  int totalPages = (int) Math.Ceiling((float) totalRecords/(float) pageSize);

  var list = (sortOrder.ToLower()=="asc") ? 
      source.OrderBy(new Func<T, IComparable>(item => (IComparable) GetPropertyValue(item, column))).Skip(pageIndex*pageSize).Take(pageSize) :
      source.OrderByDescending(new Func<T, IComparable>(item => (IComparable) GetPropertyValue(item, column))).Skip(pageIndex*pageSize).Take(pageSize);


  var jsonData = new
  {
    total = totalPages,
    page,
    records = totalRecords,
    rows = (
            from item in list
            select new
                      {
                        i = Guid.NewGuid(),
                        cell = GetGridCells(item)
                      }).ToArray()
  };

  return new JsonResult {Data = jsonData};
}

/// <summary>
/// Gets the property value.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="property">The property.</param>
/// <returns></returns>
private static object GetPropertyValue(object obj, string property) {
  System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
  return propertyInfo.GetValue(obj, null);
}
4

1 回答 1

0

DataTables 没有其列的属性。

相反,您需要调用TypeDescriptor.GetProperties()表,它将使用ITypedList实现来获取PropertyDescriptor实际列的 s。

于 2012-09-19T15:14:06.830 回答