This is a migrated thread and some comments may be shown as answers.

MVC 3.0 Razor & TreeView Help

1 Answer 34 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Khurram
Top achievements
Rank 1
Khurram asked on 07 May 2012, 11:32 AM
Hello

I have a model which looks like this:

public partial class ChartOfAccount
{
    public string ID { get; set; }
    public string ParentID { get; set; }
    public string AccountName { get; set; }
}

If the ParentID is null then that AccountName is a the root level, if ParentID contains some data, i.e. ParentID == ID then that row/Account is the child of that ParentID.

I have looked up a numerous examples on TreeView unfortunately I can only show all the rows as root of the Treeview, no sub nodes are being created.

Any help will be much appreciated!

Thanks

1 Answer, 1 is accepted

Sort by
0
DiJ8
Top achievements
Rank 1
answered on 29 May 2012, 01:36 PM
I found similar issues and then discovered there are a few things involved.

First you need to include the parent child relationship in your model:
public virtual ChartOfAccount Parent { get; set; }
public virtual ICollection<ChartOfAccount> Children { get; set; }

I'm not sure if you need to create a model map as well but I did anyway:
this.HasMany(c => c.Children)
.WithOptional(p => p.Parent)
.WillCascadeOnDelete(false);

And finally the model you send to the view needs to be just the root items "context.ChartOfAccount.Where(coa => coa.Parent == null).toList()" with the children set using the "mapping" example as supplied by Telerik.
Tags
TreeView
Asked by
Khurram
Top achievements
Rank 1
Answers by
DiJ8
Top achievements
Rank 1
Share this question
or