【发布时间】:2017-03-02 05:14:21
【问题描述】:
我是 ASP.net MVC 的新手,我已经查看了其他问题并尝试了多种方法来使用 ViewModel 填充具有 2 个 IEnumerables 的视图。似乎在控制器中使用 groupby 方法并将它们传递给 View 会产生一些问题。
总体问题是视图不接受 ListCatViewModel。它给出了错误:IEnumerable 不包含“类别”的定义,并且没有扩展方法“类别”接受“IEnumerable”类型的第一个参数
在我在 Stackoverflow 和其他来源看到的多个问题中,我们应该将两个列表与一个变量结合起来,然后将该变量传递给视图。之后我们应该引用 ViewModel。当我引用 ViewModel 时,我无法访问每个 IEnumerable(NotesList 和 RelList)中的属性。我正在寻找有关如何更改视图中的控制器和/或引用以接受 2 个单独的集合以最终创建两个表的方向。
代码总结:CatViewModel 和 RelViewModel 拥有不同的属性。 ListCatViewModel 包含 IEnumerable 集合。
public class CatViewModel
{
public string Category { get; set; }
public decimal CountOfLoans { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal TotalVolume { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal UnfundedCommitment { get; set; }
}
public class RelViewModel
{
public string RelationshipName { get; set; }
public decimal CountOfLoans { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal TotalVolume { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:c}")]
public decimal UnfundedCommitment { get; set; }
}
public class ListCatViewModel
{
public IEnumerable<CatViewModel> NotesList { get; set; }
public IEnumerable<RelViewModel> RelList { get; set; }
}
控制器:
public ActionResult Index()
{
ApplicationDbContext db = new ApplicationDbContext();
ListCatViewModel LCVM = new ListCatViewModel();
LCVM.NotesList = GetCatList();
LCVM.RelList = GetRelList();
return View(LCVM);
}
public IEnumerable<CatViewModel> GetCatList() //Category group by query
{
ApplicationDbContext db = new ApplicationDbContext();
IEnumerable<CatViewModel> CatVM = db.LoanPortfolio.GroupBy(i => i.Category)
.Select(g => new CatViewModel
{
Category = g.Key,
CountOfLoans = g.Count(),
TotalVolume = g.Sum(i => i.Volume),
UnfundedCommitment = g.Sum(i => i.Unfunded),
//Average = g.Average(i => i.Volume)
})
.OrderBy(c => c.Category)
.AsEnumerable();
return CatVM;
}
public IEnumerable<RelViewModel> GetRelList()
{
ApplicationDbContext db = new ApplicationDbContext();
IEnumerable<RelViewModel> RelVM = db.LoanPortfolio.GroupBy(i => i.RelationshipName)
.Select(g => new RelViewModel
{
RelationshipName = g.Key,
CountOfLoans = g.Count(),
TotalVolume = g.Sum(i => i.Volume),
UnfundedCommitment = g.Sum(i => i.Unfunded),
//Average = g.Average(i => i.Volume)
})
.OrderBy(c => c.RelationshipName)
.AsEnumerable();
return RelVM;
}
查看:
查看:
@model IEnumerable<Risk.ViewModel.ListCatViewModel>
@{
ViewBag.Title = "GroupByLoanPAllowanceB";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Category</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.NotesList.Category)
</th>
<th>
@Html.DisplayNameFor(model => model.NotesList.CountOfLoans)
</th>
<th>
@Html.DisplayNameFor(model => model.NotesList.TotalVolume)
</th>
<th>
@Html.DisplayNameFor(model => model.NotesList.UnfundedCommitment)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.NotesList.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.CountOfLoans)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalVolume)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnfundedCommitment)
</td>
</tr>
}
</table>
【问题讨论】:
-
@foreach (var item in Model.NotesList)和@foreach (var item in Model.RelList) -
或者创建一个编辑器或显示模板,分别使用
EditorFor或DisplayFor。 -
如何在每个 foreach 代码块上方添加列名的单次迭代?我需要能够在每个列表中引用它们。
标签: c# asp.net-mvc linq