您好我将数据表传递给方法“AsJqGridResult”,但“GetPropertyValue”方法返回错误,因为“System.Reflection.PropertyInfo propertyInfo”返回NULL。
传递数据表或任何其他问题是否有任何问题,请帮助....
控制器
[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);
}
答案 0 :(得分:0)
DataTables没有列的属性。
相反,您需要在表格上调用TypeDescriptor.GetProperties()
,这将使用ITypedList
实现来获取实际列的PropertyDescriptor
。