我是ASP.net MVC的新手,我已经查看了其他问题并尝试了多种方法,使用ViewModel使用2个IEnumerables填充视图。似乎在控制器中使用groupby方法并将它们传递给View已经产生了一些问题。
整体问题是视图不接受ListCatViewModel。它给出了错误:IEnumerable不包含'Category'的定义,并且没有扩展方法'Category'接受类型'IEnumerable'的第一个参数
在我在Stackoverflow和其他源上看到的多个问题中,我们应该将两个列表与变量组合,然后将该变量传递给View。之后我们应该引用ViewModel。当我引用ViewModel时,我无法访问每个IEnumerable(NotesList和RelList)中的属性。我正在寻找有关如何更改View中的控制器和/或引用的方向,以接受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>
答案 0 :(得分:0)
您的观点不是IEnumerable<Risk.ViewModel.ListCatViewModel>
,现在是ListCatViewModel
尝试:
@model Risk.ViewModel.ListCatViewModel
@{
ViewBag.Title = "GroupByLoanPAllowanceB";
Layout = "~/Views/Shared/_Layout.cshtml";
}
在遍历列表时,您需要从Model
@foreach (var item in Model.NotesList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.CountOfLoans)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalVolume)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnfundedCommitment)
</td>
</tr>
}
答案 1 :(得分:0)
假设您只是尝试显示数据,请在Views -> Shared -> DisplayTemplates
文件夹中创建一些模板(您可能需要创建它),然后使用DisplayFor
。以下是CatViewModel的示例:
@model Your.Fully.Qualified.Namespace.CatViewModel
<tr>
<td>
@Model.Category
</td>
<td>
@Model.CountOfLoans
</td>
<td>
@Model.TotalVolume
</td>
<td>
@Model.UnfundedCommitment
</td>
</tr>
@model Risk.ViewModel.ListCatViewModel
@{
ViewBag.Title = "GroupByLoanPAllowanceB";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Category</h2>
<table class="table">
<tr>
<th>
Category
</th>
<th>
Count Of Loans
</th>
<th>
Total Volume
</th>
<th>
Unfunded Commitment
</th>
</tr>
@Html.DisplayFor(m => m.NotesList)
</table>
当您拨打EditorFor
或DisplayFor
时,Razor引擎非常智能,可以查找与模型名称相同的部分视图(在上述文件夹中)。因此,如果属性类型为Foo
,并且您有一个名为Foo.cshtml
的视图,则它将用于该属性。如果您提供集合,它还将遍历集合并为每个项目生成标记。
如果您不想遵循我上面提到的惯例,还有an overload that allows you to specify the template to use。