简体   繁体   English

如何在asp.net mvc 2中创建树视图?

[英]How do I create a treeview in asp.net mvc 2?

I have a couple of database tables, one for Folders and one for Files, with the following schemas. 我有几个数据库表,一个用于文件夹,一个用于文件,具有以下模式。

Folders 资料夹

  • FolderID 资料夹编号
  • FolderName 文件夹名称
  • DateCreated 创建日期
  • ParentFolderID ParentFolderID

Files 档案

  • FileID 文件ID
  • FileName 文档名称
  • FileExtension 文件扩展名
  • Description 描述
  • FolderID 资料夹编号
  • DateUploaded 更新日期
  • DownloadCount 下载数

Basically I need to know how I can query these in such a way that the JSON returned, could be used with the jQuery Treeview plugin , or jqTree . 基本上,我需要知道如何以这样的方式查询这些内容,使得返回的JSON可以与jQuery Treeview插件jqTree一起使用

I have two classes, File and Folder, that can be used to create File and Folder objects, from the data returned from the database, but I'm not sure how to go about returning it as json, so that folders contain their sub-folders and all the files show up in the correct folders. 我有两个类,File和Folder,可用于从数据库返回的数据中创建File和Folder对象,但是我不确定如何将其作为json返回,因此文件夹包含其子目录。文件夹和所有文件显示在正确的文件夹中。

Any help would be appreciated, I search and searched, but all I could find was an example that used ASP.NET MVC 3, and unfortunately I'm stuck using ASP.NET MVC2 on this project. 我搜索了所有帮助,将不胜感激,但是我能找到的只是一个使用ASP.NET MVC 3的示例,不幸的是,我在该项目上使用ASP.NET MVC2。

Thanks in advance. 提前致谢。

public class Folder
{
    public int FolderID { get; set; }
    public string FolderName { get; set; }        
    public IList<Folder> Subfolders { get; set; }
    public IList<File> Files { get; set; } 
    public bool IsRootFolder
    {
        get { return Subfolders.Count == 0; }
    }
}

public class File
{
    public int FileID { get; set; }
    public int FolderID { get; set; }
    public string FileName { get; set; }
    public string Exstension { get; set; }
    public string Description { get; set; }
    public DateTime? UploadDate { get; set; }
    public int DownloadCount { get; set; }
}

The HTML for a treeview is just nested unordered lists (ul and li elements). 树视图的HTML只是嵌套的无序列表(ul和li元素)。 So just generate this from your JSON data, then style the outermost list using your plugin. 因此,只需从您的JSON数据生成此代码,然后使用您的插件设置最外层列表的样式即可。

Eg if you're using the jQuery treeview plugin, you could generate: 例如,如果您使用的是jQuery treeview插件,则可以生成:

<ul id="tree">
    <li>Node 1</li>
    <li>Node 2</li>
        <ul>
            <li>Node 2.1</li>
            <li>Node 2.2</li>
        </ul>
    <li>Node 3</li>
</ul>

then style using: 然后使用以下样式:

$("#tree").treeview();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM