Hi,
I am trying to use a hierarchical grid using MVC 5. I am suppose to have three levels of data in the grid - parent , child and grand child.
I am able to bind the parent but not the child using Load on demand from the controller.
--Modelpublic class ParentViewModel{ public int PId {get; set;} public string Name {get; set;} public ICollection ChildDetails}
Public class ChildViewModel{ public int CId {get; set;} public int PId {get; set;} public int CData {get; set;}}
--View@model Infragistics.Web.Mvc.GridModel
(Html.Infragistics().Grid(Model))
--Controller
private GridModel GetGridModel(){
GridModel model = new GridModel(); model.AutoGenerateColumns = false; model.AutoCommit = true; model.PrimaryKey = "PId"; model.AutoGenerateLayouts = false; model.ID = "igGrid";
model.LoadOnDemand = true;
model.Columns.Add(new GridColumn { Key = "PId", Hidden = true, DataType = "number" }); model.Columns.Add(new GridColumn { Key = "Name", HeaderText = "Parent Name ", DataType = "string" }); model.DataSourceUrl = (Url.Action("BindParentDetails"));
GridColumnLayoutModel childModel = new GridColumnLayoutModel(); childModel.Key = "ChildDetails"; childModel.PrimaryKey = "PID"; childModel.ForeignKey = "CId"; childModel.AutoGenerateColumns = false; childModel.Columns.Add(new GridColumn { Key = "CId", HeaderText = "Child ID", DataType = "number" }); childModel.Columns.Add(new GridColumn { Key = "CData", HeaderText = "Child Data", DataType = "number" }); childModel.DataSourceUrl = Url.Action("BindChildDetails");
model.ColumnLayouts.Add(childModel); return model;}
public JsonResult BindParentDetails() { GridModel rModel = GetGridModel();
var p = _service.GetPDetails();
rModel.DataSource = p.AsQueryable(); var res = rModel.GetData(); return res; }
public JsonResult BindChildDetails(string path, string layout) { var pid = path.Substring(path.LastIndexOf(':') + 1);
GridModel grd = GetGridModel();
var p = _service.GetChildDetails(pId); grd.DataSource = p.AsQueryable(); var res = grd.GetData(path, layout); return res; }
When I run the solution it binds the parent grid correctly. On clicking the + icon it goes into BindChildDetails function and breaks on:
var res = grd.GetData(path, layout);
An exception of type 'System.NullReferenceException' occurred in Infragistics.Web.Mvc.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
It has correct values in path and layout
grd.DataSource is set correctly with the data from the DB.
The version of Infragistics.Web.Mvc we are using is 5.16.2.2040
Can anyone please help me with this?
Hello Singh,
Thank you for the update and your sample. I have looked into and have found why you are having this issue. In your BindChildDetails method there are two changes to make. First is when you are creating the child grid layout you are using the GridModel and the GetGridModel for the parent where as instead you should use the following:
GridColumnLayoutModel grd = GetChildGridModel();
Then when you call GetData on grd you are passing in the path and layout when you already filtering the data from the datasource by the parent id. So you will either want stop pre-filtering it or instead call:
var res = grd.GetData();
So when you are doing the filtering yourself it would look like the following:
public JsonResult BindChildDetails(string path, string layout) { var pid = path.Substring(path.LastIndexOf(':') + 1); int parentId = int.Parse(pid);
var p = PopulateChild(parentId); grd.DataSource = p.AsQueryable(); var res = grd.GetData(); return res; }
Thank you.
I have emailed you.
Thank you for the update. Can you please attach a running sample, so I may debug it and see what specifically isn’t working in your scenario? When attaching you can attach with dummy data if needed. As well make sure to delete the packages folder so it can be attached to the forums. If you do not wish to attach it to the forums. You can set it to support@infragistics.com with the case number CAS-183529-C8M9T5 in the subject.
Thanks Mike
I have tried as you suggested but still getting the same error
--Modelpublic class ParentViewModel{ public int PId {get; set;} public string Name {get; set;} public ICollection<ChildViewModel> ChildDetails}
-- View@model Infragistics.Web.Mvc.GridModel
GridColumnLayoutModel childModel = GetChildGridModel();
private GridModel GetChildGridModel(){ GridColumnLayoutModel childModel = new GridColumnLayoutModel(); childModel.Key = "ChildDetails"; childModel.PrimaryKey = "CID"; childModel.ForeignKey = "PId"; childModel.AutoGenerateColumns = false; childModel.Columns.Add(new GridColumn { Key = "CId", HeaderText = "Child ID", DataType = "number" }); childModel.Columns.Add(new GridColumn { Key = "CData", HeaderText = "Child Data", DataType = "number" }); childModel.DataSourceUrl = Url.Action("BindChildDetails"); return childModel ;
} public JsonResult BindParentDetails() { GridModel rModel = GetGridModel();
Thank you for contacting Infragistics!
Looking at your code it appears you return the child and parent model/layout in GetGridModel, when you should have different methods for each level. Here is documentation and a sample on setting up Load on Demand in the igHierarchicalGrid:
https://www.igniteui.com/help/ighierarchicalgrid-load-on-demand
https://www.igniteui.com/hierarchical-grid/load-on-demand