我正在尝试创建一个不同年份呈现不同html表的视图。
用于视图的模型是:
public class VisualizzaFattureViewModel
{
public FileInfo FatturaFile { get; set; }
public int Year { get; set; }
}
这是控制器动作:
public ActionResult VisualizzaFatture()
{
DirectoryInfo fattureFilesDirectory = null;
FileInfo[] fattureFiles = null;
try
{
string fattureFilesPath = Server.MapPath("~/Documents/Fatture/");
fattureFilesDirectory = new DirectoryInfo(fattureFilesPath);
fattureFiles = fattureFilesDirectory.GetFiles();
}
catch (DirectoryNotFoundException exp)
{
throw exp;
}
catch (IOException exp)
{
throw exp;
}
fattureFiles = fattureFiles.OrderByDescending(f => f.Name).ToArray();
List<VisualizzaFattureViewModel> fatture = new List<VisualizzaFattureViewModel>();
foreach (var file in fattureFiles)
{
var model = new VisualizzaFattureViewModel();
model.FatturaFile = file;
model.Year = int.Parse(fattureFiles.First().Name.Split('_').ElementAt(1));
fatture.Add(model);
}
return View(fatture);
我希望视图以这种方式显示数据,例如:
[2016]
|--------------------------------------|
|file5.name |
|--------------------------------------|
[2015]
|--------------------------------------|
|file4.name |
|--------------------------------------|
|file3.name |
|--------------------------------------|
[2013]
|--------------------------------------|
|file2.name |
|--------------------------------------|
|file1.name |
|--------------------------------------|
我怎样才能做到这一点?
答案 0 :(得分:2)
我认为你应该重新定义你的模型以包含你所描述的表的信息。类似的东西:
// Dictionary key is the year
// Dictionary value is a list of FileInfo structures for that year
public class VisualizzaFattureViewModel
{
public Dictionary<int, List<FileInfo>> Items { get; set; }
}
然后填写解析年份的模型项:
var model = new VisualizzaFattureViewModel();
model.Items = new Dictionary<int, List<FileInfo>>();
foreach (var file in fattureFiles)
{
var year = int.Parse(file.Name.Split('_').ElementAt(1));
if (!model.Items.ContainsKey(year))
{
model.Items.Add(year, new List<FileInfo>());
}
model.Items[year].Add(file);
}
return View(model);
在您的视图中,您只需制作两个foreach语句,以便首先迭代多年,然后再通过每年的项目并相应地构建表格标记。如果您愿意,也可以按年份降序排序字典(因为这是您在示例表中显示的方式)。
希望它有所帮助。如果您还需要查看样本,请告诉我。
答案 1 :(得分:0)
ActionResult
中的:
return View(fattureFiles.ToList());
View
中的:
@model List<FileInfo>
@foreach (var item in Model.GroupBy(x => x.Name.Split('_').ElementAt(1)).OrderByDescending(x => x.Key))
{
<h5>@item.Key</h5>
<ul class="list-group">
@foreach (var file in item)
{
<li class="list-group-item">@file.Name</li>
}
</ul>
}
使用你的模型但改变它:
public class VisualizzaFattureViewModel
{
public FileInfo[] FatturaFiles { get; set; }
public string Year { get; set; }
}
和ActionResult
:
var grp = fattureFiles.GroupBy(f => f.Name.Split('_').ElementAt(1));
var fatture = grp.Select( x=> new VisualizzaFattureViewModel() {Year = x.Key, FatturaFiles = x.ToArray() }).ToList();
return View(fatture);
视图:
@model List<VisualizzaFattureViewModel>
@foreach (var item in Model.OrderByDescending(x => x.Year))
{
<h5>@item.Year</h5>
<ul class="list-group">
@foreach (var file in item.FatturaFiles)
{
<li class="list-group-item">@file.Name</li>
}
</ul>
}