We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

NullReferenceException & Argument Exception - Treeview Binding

I have to bind treeview to my custom model. But it is giving me "NullReferenceException: Object reference not set to an instance of an object".

Below is my model -

public class TreeViewModel
{
   public string Id { get; set; }
   public string ParentId { get; set; }
   public TemplateName { get; set; }
}

IEnumerabele List -

IEnumerable<TreeViewModel> list = new List<TreeViewModel>() {
   new TreeViewModel {Id="1", ParentId="0", TemplateName="Coil Tubing" },
   new TreeViewModel {Id="2", ParentId="0", TemplateName="CH Jobs" },
   new TreeViewModel { Id="3", ParentId="0", TemplateName="OH Services"},
   new TreeViewModel {Id="4", ParentId="3", TemplateName="RDT" },
   new TreeViewModel {Id="5", ParentId="3", TemplateName="Seismic" }
};

And the code to bind treeview is

@model IEnumerable<TreeViewModel>
@{
    ViewBag.Title =
"Index";
}
<h2>
Index</h2>

@{ Html.Syncfusion().TreeView("MyTreeView")
   .DataSource(Model)
   .BindTo(bindto =>
      bindto.Id(
"Id")
            .ParentId(
"ParentId")
            .Text(
"TemplateName"))
   .Render();
}

I changed data type of model properties "Id" & "ParentId" from string to int, then other exception "ArgumentException: Argument types do not match" comes.

Atached are the stack trace for both the exceptions.



Exception_5a60d2cb.zip

1 Reply

ME Meena Syncfusion Team June 20, 2012 07:50 AM UTC

Hi Ragni,

 

Thanks for contacting Syncfusion forums.

 

Query: "NullReferenceException: Object reference not set to an instance of an object".

 

The reported issue occurred because the ID and ParentID values are declared with string types, we suggest you to use int? type for ID fields.

Query2: changed data type of model properties "Id" & "ParentId" from string to int, then other exception "ArgumentException: Argument types do not match" comes.

To overcome this issue, we suggest you to set null value for ParentID instead of “0”. Please refer the below code snippet.

 

<code>

[Model]

public class TreeViewSampleModel

{

        public int? Id { get; set; }

        public int? ParentId { get; set; }

        public string TemplateName { get; set; }

}

[List]

 

new TreeViewSampleModel(){Id=1, ParentId= null, TemplateName="Coil Tubing" };

new TreeViewSampleModel() { Id = 2, ParentId = null, TemplateName = "CH Jobs" };

new TreeViewSampleModel() { Id = 3, ParentId = null, TemplateName = "OH Services" };

new TreeViewSampleModel(){Id=4, ParentId=3, TemplateName="RDT" };

new TreeViewSampleModel() {Id=5, ParentId=3, TemplateName="Seismic" };

 

</code>

 

We have also prepared a simple sample to recover the above issues and the sample can be downloaded from the below link.

 

 

Please let us know if you have any other concerns.

 

Regards,

Meena



TreeView_ModelBinding_cdb488.zip

Loader.
Live Chat Icon For mobile
Up arrow icon