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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

NullReferenceException & Argument Exception - Treeview Binding

Thread ID:

Created:

Updated:

Platform:

Replies:

104061 Jun 19,2012 05:38 AM UTC Jun 20,2012 07:50 AM UTC ASP.NET MVC (Classic) 1
loading
Tags: Tools
Mragni
Asked On June 19, 2012 05:38 AM UTC

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

Meena [Syncfusion]
Replied On 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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;