这是我的模型:
public class ViewInvoice
{
public string ClientLocation { get; set; }
public List<DetailsGroup> Details { get; set; }
public class DetailsGroup
{
public List<string> Product { get; set; }
public List<string> ProductSize { get; set; }
public List<string> PackageType { get; set; }
public List<DateTime> OrderDate { get; set; }
public List<DateTime> DeliveryDate { get; set; }
public List<int> OrderNumber { get; set; }
public List<decimal> Price { get; set; }
public List<int> ItemQuantity { get; set; }
}
}
我正在尝试在我的 Razor View 中的表格中显示此模型。这是代码:
@using MyModel.MyTools.Orders.SumOrder
@model SumOrder
@{
ViewBag.Title = "View Invoice";
}
<h2>View Invoice</h2>
<table>
@foreach(var prod in Model.OCI)
{
<tr>
<td>
@prod.ClientLocation
</td>
</tr>
foreach (var orderItem in prod.Details)
{
<tr>
<td>
@orderItem.Product
</td>
<td>
@orderItem.ItemQuantity
</td>
</tr>
}
}
</table>
表格中的第一行显示正确,这是一个城市的名称,但在下一行我得到这个:
System.Collections.Generic.List1[System.String] System.Collections.Generic.List
1[System.Int32]
有人可以向我解释为什么我无法以可读格式返回列表,以及如何纠正此问题吗?
这是我用来对 ViewInvoice 模型的列表进行分组的代码:
// group the cartItems according to location
List<ViewInvoice> ordersGrouped = cartItems.GroupBy(c => new
{c.ClientLocation})
.OrderBy(c => c.Key.ClientLocation).Select(s =>
new ViewInvoice()
{
ClientLocation = s.Key.ClientLocation,
Details = new List<ViewInvoice.DetailsGroup>()
{
new ViewInvoice.DetailsGroup()
{
Product = s.Select(p => p.Product).ToList(),
ItemQuantity = s.Select(p => p.ItemQuantity).ToList(),
DeliveryDate = s.Select(p => p.DeliveryDate).ToList(),
OrderDate = s.Select(p => p.OrderDate).ToList(),
OrderNumber = s.Select(p => p.OrderNumber).ToList(),
PackageType = s.Select(p => p.PackageType).ToList(),
Price = s.Select(p => p.Price).ToList(),
ProductSize = s.Select(p => p.ProductSize).ToList()
}
}
}).ToList();
最佳答案
好吧,我的问题终于解决了。我一开始对这个问题想得太多了。我简化了我的模型,并向我的 View 添加了一些简单的逻辑。
这是更新后的模型:
public class ViewInvoice
{
public string ClientLocation { get; set; }
public List<string> Product { get; set; }
public List<string> ProductSize { get; set; }
public List<string> PackageType { get; set; }
public List<DateTime> OrderDate { get; set; }
public List<DateTime> DeliveryDate { get; set; }
public List<int> OrderNumber { get; set; }
public List<decimal> Price { get; set; }
public List<int> ItemQuantity { get; set; }
}
用于对模型列表进行分组的更新代码:
// group the cartItems according to location
List<ViewInvoice> ordersGrouped = cartItems.GroupBy(c => new
{c.ClientLocation})
.OrderBy(c => c.Key.ClientLocation).Select(s =>
new ViewInvoice()
{
ClientLocation = s.Key.ClientLocation,
Product = s.Select(p => p.Product).ToList(),
ItemQuantity = s.Select(p => p.ItemQuantity).ToList(),
DeliveryDate = s.Select(p => p.DeliveryDate).ToList(),
OrderDate = s.Select(p => p.OrderDate).ToList(),
OrderNumber = s.Select(p => p.OrderNumber).ToList(),
PackageType = s.Select(p => p.PackageType).ToList(),
Price = s.Select(p => p.Price).ToList(),
ProductSize = s.Select(p => p.ProductSize).ToList()
}).ToList();
以及更新后的 View :
@using MyModel.MyTools.Orders.SumOrder
@model SumOrder
@{
ViewBag.Title = "View Invoice";
}
<h2>View Invoice</h2>
@{
int i = 0;
}
<table>
@foreach(var mod in Model.OCI)
{
var modCount = @mod.Product.Count();
<tr>
<th>@mod.ClientLocation</th>
</tr>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
foreach (var items in mod.Product)
{
<tr>
<td>
@mod.Product.ElementAtOrDefault(i)
</td>
<td>
@mod.Price.ElementAtOrDefault(i)
</td>
</tr>
i++;
}
}
</table>
这个解决方案显然允许我迭代模型,沿途复制任何所需的行或单元格。这两天就这个问题玩了俄罗斯轮盘赌。希望这可以为面临此问题的其他人节省一些时间。
关于c# - 在 ASP.Net MVC View 中显示模型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12888484/