尝试使用mvc和viewmodel创建树视图

时间:2014-02-04 03:26:01

标签: asp.net-mvc mvvm

我尝试使用jsTree插件进行树视图。

所以在viewmodel中我有一个函数Ge​​tTreeViewList-它返回一个ViewModel。它应该获得第一个父节点0然后它调用GetChildNodes()函数 - 它应该采用子节点。 但是当我尝试创建CategoryViewModel()类型的rootNode时 - 它给了我一个异常,该对象没有设置为对象的实例

 public class CategoryViewModel
{
    public List<CategoryLanguages> lstCategories { get; set; }

    private readonly ICategoriesLanguagesRepository catRepository;

    public CategoryViewModel() : this(new CategoryLanguageRepository())
    {

    }

    public CategoryViewModel(ICategoriesLanguagesRepository catRepository)
    {
        this.catRepository = catRepository;
    }
    public string EmployeeCode { get; set; }
    public string EmployeeName { get; set; }
    public IList<CategoryViewModel> ChildNodes { get; set; }

    public CategoryViewModel GetTreeVeiwList()
    {

        CategoryLanguages parrentNodes = catRepository.GetAllParrentCaregories();

            CategoryViewModel rootNode = new CategoryViewModel()
                                       {
                                           EmployeeCode = parrentNodes.CategoryLanguagesID.ToString(),
                                           EmployeeName = parrentNodes.Title
                                       };
            GetChildNode(rootNode, parrentNodes);

            return rootNode;              

    }
    private void GetChildNode(CategoryViewModel rootNode,CategoryLanguages item)
    {

        if (rootNode != null)
        {
             List<CategoryLanguages> childNodes= catRepository.GetChildNodes(item);

             if (childNodes.Count > 0)
             {
                 foreach (var childRootNode in childNodes)
                 {
                     CategoryViewModel chdnode = new CategoryViewModel { EmployeeCode = childRootNode.CategoryLanguagesID.ToString(), EmployeeName = childRootNode.Title };
                     GetChildNode(chdnode, childRootNode);
                     rootNode.ChildNodes.Add(chdnode);
                 }
             }

}

这是我的控制器

public class CategoryController:Controller {

public ActionResult Index()
{
    CategoryViewModel ob = new CategoryViewModel();
    ob.GetTreeVeiwList();
    return View(ob);
}

}

0 个答案:

没有答案