如何呈现MVC视图页面单向链表 [英] How to render singly Linked list in MVC View page
问题描述
由于我试图创造MVC一个TreeView文件夹结构。我有类文件类似下面的。
As I am trying to create a TreeView folder structure in MVC. I have class file something like the below.
类文件
public class TreeViewFolder
{
public string FolderPath { get; set; }
public string FolderName { get; set; }
public List<TreeViewFolder> MyTreeList { get; set; }
}
我要呈现在上面MVC视图列表中。我不知道如何在MVC视图呈现单链表的数据。任何帮助将AP preciated。
I need to render that above list in MVC View. I have no idea how to render single linked list data in MVC View. Any help will be appreciated.
感谢
推荐答案
您可以创建一个使用递归构建一个扩展方法&LT; UL&GT;
和&LT;立GT;
元素,以显示文件夹的层级
You could create an extension method that uses recursion to build <ul>
and <li>
elements to show the hierarchy of folders
public static class FolderTreeExtensions
{
public static MvcHtmlString FolderTree(this HtmlHelper helper, TreeViewFolder folder)
{
return MvcHtmlString.Create(TreeLeaf(folder));
}
// Recursive function
private static string TreeLeaf(TreeViewFolder folder)
{
StringBuilder html = new StringBuilder();
TagBuilder div = new TagBuilder("div");
div.InnerHtml = folder.FolderName;
html.Append(div.ToString());
if (folder.MyTreeList != null)
{
foreach (TreeViewFolder subFolder in folder.MyTreeList)
{
html.Append(TreeLeaf(subFolder));
}
}
TagBuilder item = new TagBuilder("li");
item.InnerHtml = html.ToString();
TagBuilder container = new TagBuilder("ul");
container.InnerHtml = item.ToString();
return container.ToString();
}
}
然后在你的控制器,初始化和填充和 TreeViewFolder
的实例,并在视图
@model TreeViewFolder
....
@Html.FolderTree(Model)
然后样式元素,以满足您的要求。
Then style the elements to suit your requirements
请注意:无论是在视图中添加使用
语句或添加到&LT的引用;命名空间&GT;
部分 web.config中的
Note: either add a using
statement in the view or add a reference to the <namespaces>
section of web.config
这篇关于如何呈现MVC视图页面单向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!