这是我的模特:
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; }
}
}
我正在尝试在剃刀视图中的表格中显示此模型。这是代码:
@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.List 1[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();
答案 0 :(得分:2)
好的,我终于解决了我的问题。我最初过度思考了这个问题。我简化了我的模型,并为我的观点添加了一些简单的逻辑。
以下是更新后的模型:
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();
和更新后的观点:
@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>
这个解决方案显然允许我遍历模型,沿途重现任何所需的行或单元格。过去两天在这个问题上玩过俄罗斯轮盘赌。希望这为面临这个问题的其他人节省一些时间。
答案 1 :(得分:1)
您的模型对象没有任何意义。为什么您的每个属性(如价格和数量)都是集合?单个订单行项目对象每个属性只有一个值,而不是集合。您的发票对象已经占多个Details对象,但是单个详细信息对象不应该将其属性作为列表。删除列表&lt;&gt;来自你所有的财产,它应该解决它。
答案 2 :(得分:0)
因为字符串列表与字符串不同。 你看到的是List类的ToString()方法返回的内容,默认值。
要以逗号分隔的显示方式从列表中获取元素,请使用:
string.Join(",", orderItem.Product.ToArray())
string.Join(",", orderItem.ItemQuantity.ToArray())
每当你想打印某个东西时,将调用该对象的ToString()方法。 这个在List&lt;&gt;上定义的ToString()方法class不知道你希望如何显示数据,看看列表如何能够获得多个String或int或Date。您可能想要添加&lt; p&gt;在每个值前面和&lt; / p&gt;以每个值为例。
第一行正确显示,因为ClientLocation是一种简单类型。 有关简单类型的更多信息,请参阅simple types