This is a migrated thread and some comments may be shown as answers.
Strange binding Behavior
1 Answer 40 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Nathan Taylor
Top achievements
Rank 1
Nathan Taylor asked on 09 Jun 2010, 10:32 PM
I am trying to use the TreeView component to mimic a file and folder tree. I've already written the code to pull the directory structure, but when the data is bound to the TreeView it is skipping objects in the output even though I have explicitly defined a binding instruction for them. If I manually create the folder and file information object in the view (rather than pulling it from the file system) the exact same binding code renders the TreeView correctly. I have confirmed that the data being returned by the file and folder iterator is properly constructed, but for some reason the FileItem objects never make it into the markup.

Here is the make up of my model items

public abstract class PathItem 
    public string Name { getset; } 
    public string FullPath { getset; } 
 
public class DirectoryItem : PathItem 
    public PathItem[] Paths { getset; } 
 
public class FileItem : PathItem 
    public long Size { getset; } 

Here is the code I am using to render the view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<PathItem>>" %> 
 
<% Html.Telerik().TreeView() 
    .Name("Files") 
    .BindTo(Model, mappings => { 
        mappings.For<DirectoryItem>(binding => binding 
                .ItemDataBound((item, directory) => { 
                    item.Text = directory.Name; 
                }) 
                .Children(directory => directory.Paths)); 
 
        mappings.For<FileItem>(binding => binding 
                .ItemDataBound((item, file) => { 
                    item.Text = file.Name; 
                    item.Url = file.FullPath; 
                })); 
    }).Render(); %> 
 

As I said above, the FileItem is never rendered in the output even though I have confirmed that the data in the Model contains multiple FileItems and the debugger is stepping into the For<FileItem>() binding with a valid FileItem reference.

As a test, if I pass the following object to the BindTo() method everything works as expected:

var data = new PathItem[] { 
      new DirectoryItem() { 
              FullPath = ""
              Name = "Search Engines"
              Paths = new PathItem[] { 
                         new FileItem() {FullPath = "http://www.google.com", Name = "Google"}, 
                         new FileItem() {FullPath = "http://www.bing.com", Name = "Bing"}, 
                         new FileItem() {FullPath = "http://www.yahoo.com", Name = "Yahoo"}, 
                         new DirectoryItem() { 
                             FullPath = ""
                             Name = "Old"
                             Paths = new PathItem[] { 
                                new FileItem() {FullPath = "http://www.altavista.com", Name = "AltaVista"}, 
                             } 
                         } 
              } 
       }, 
       new FileItem() {FullPath = "http://www.digg.com", Name = "Digg"
}; 


So to recap, the code I am binding the TreeView with works fine for all DirectoryInfo objects regardless, but fails to render any output for FileInfo objects if the FileInfo objects are being created dynamically rather than manually.

Any thoughts?

1 Answer, 1 is accepted

Sort by
0
Nathan Taylor
Top achievements
Rank 1
answered on 09 Jun 2010, 11:25 PM
So I found the problem and I firmly believe it is something that warrants documentation and/or an exception message.

While populating my TreeView items and binding each FileItem I was setting the item.Url to the FileItem's full directory path ("C:\Path\to\foo.txt") rather than a valid Url. This invalid Url was causing the TreeView to completely throw out the item being bound without any sort of error message or obvious indication of the reason.

Is that a desirable behavior?
Tags
TreeView
Asked by
Nathan Taylor
Top achievements
Rank 1
Answers by
Nathan Taylor
Top achievements
Rank 1
Share this question
or