有人知道为什么以下c#mvc.net代码在将列表数据绑定到jqgrid时会给出空引用异常吗?
这是一个用于说明问题的缩减示例。
如果我改变了 DataField =“MyClass.Id” 至 DataField =“Id” 它绑定得很好,但在我的真实代码中,我试图绑定到一个具有2个子对象的对象,并且我试图在同一网格上显示两者的数据。
任何建议都非常感谢。 感谢
(我看过这篇文章,但没有得到答案:jqgrid List of objects (with sub objects) as datasource)
internal class MyWrapper
{
public MyClass MyClass { get; set; }
public int Id { get; set; }
}
internal class MyClass
{
public int Id { get; set; }
}
public class TestController : Controller
{
public ActionResult Index()
{
return View(GetGrid());
}
public JsonResult SearchGridDataRequested()
{
return GetGrid().DataBind(GetModel().AsQueryable());
}
private JQGrid GetGrid()
{
return new JQGrid
{
ID = "MyGrid",
DataUrl = Url.Action("SearchGridDataRequested"),
Width = Unit.Pixel(700),
Columns = new List<JQGridColumn>
{
new JQGridColumn
{
DataField = "MyClass.Id",
PrimaryKey = true,
DataType = typeof (int)
}
}
};
}
private static IEnumerable<MyWrapper> GetModel()
{
return new List<MyWrapper>
{
new MyWrapper {Id = 1, MyClass = new MyClass {Id = 11}},
new MyWrapper {Id = 2, MyClass = new MyClass {Id = 12}},
new MyWrapper {Id = 3, MyClass = new MyClass {Id = 13}}
};
}
}