[英]Display only one category with mutliple files in treeview
我有一個Treeview,它顯示類別以及與之相關的文件。 我要運行的是每個類別顯示多次,並顯示每次上載時添加的新文件。 我想要做的是一次顯示類別,然后在其下的每個項目顯示。
我試圖將foreach移到文件鏈接的上方,但是后來我不知道如何顯示類別名稱。
這是我當前的標記
<div role="tabpanel" class="tab-pane" id="tree">
@if (Model.Collaboration.Files.Any())
{
foreach (var file in Model.Collaboration.Files)
{
<div class="treeview">
<ul>
<li>
<input type="checkbox" />
<label>
@if (file.Category != null)
{
<span>@file.Category.Name</span>
}
else
{
<span>Uncategorized</span>
}
</label>
<ul>
<li>
@Html.ActionLink(file.Filename, "ShowFile", "Attachments", new { @id = file.Id, collaborationId = Model.Collaboration.Id }, new { target = "_blank" })
</li>
</ul>
</li>
</ul>
</div>
}
}
</div>
我的模特
public class AttachmentCategory
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
}
雖然可以在視圖中包括條件語句以將category.Name
屬性存儲在變量中,並在每次迭代中測試category.Name
與存儲的值匹配(如果匹配,則省略<label>
標記),這將是相當混亂,並且還要求您的文件按category.Name
排序。 解決此問題的最簡單方法是使用表示您要顯示的內容的視圖模型。 您尚未顯示所有模型的全部詳細信息,因此這是一個簡化的示例
public class FileVM
{
public int ID { get; set; }
public string Name { get; set; }
}
public class CategoryVM
{
public string ID { get; set; }
public string Name { get; set; }
public IEnumerable<FileVM> Files { get; set; }
}
然后在控制器中
public ActionResult Details()
{
List<CategoryVM> model = new List<CategoryVM>();
// populate your collection of categories, and for each category, populate its collection of files
return View(model);
}
視圖
@model IEnumerable<CategoryVM>
<ul>
@foreach(CategoryVM category in Model)
{
<li>
<span>@Html.DisplayFor(m => category.Name)</span>
<ul>
@foreach(FileVM file in category.Files)
{
<li>
@Html.ActionLink(file.Filename, "ShowFile", "Attachments", new { id = file.ID, collaborationId = category.ID }, new { target = "_blank" })
</li>
}
</ul>
<li>
}
<ul>
邊注:
name
屬性,因此不會回發任何內容) <label>
標簽沒有意義-它沒有與任何控件關聯,因此它實際上不是標簽。 CategoryVM.Name
屬性,而不是在視圖中使用不必要的if
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.