C# 在视图中分组和显示模型
我很难将列表分组,然后构建一个表示该列表的模型,并在视图中的表中显示结果。例如: 订购项目列表C# 在视图中分组和显示模型,c#,asp.net-mvc,C#,Asp.net Mvc,我很难将列表分组,然后构建一个表示该列表的模型,并在视图中的表中显示结果。例如: 订购项目列表 日期 客户ID 位置 项目 价格 数量 如果我想按位置对列表进行分组,我将如何正确地建模并在表中显示该列表?如果我想按两个属性对列表进行分组,例如Location和CustomerId,该怎么办 这是我的模型: public class ViewInvoice { public string ClientLocation { get; set; } public List<DetailsGrou
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; }
}
}
公共类视图发票
{
公共字符串ClientLocation{get;set;}
公共列表详细信息{get;set;}
公共类详细信息组
{
公共列表产品{get;set;}
公共列表ProductSize{get;set;}
公共列表PackageType{get;set;}
公共列表OrderDate{get;set;}
公共列表交付日期{get;set;}
公共列表顺序号{get;set;}
公开标价{get;set;}
公共列表项数量{get;set;}
}
}
我试图在razor视图中的表中显示此模型。这是代码:
@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>
@使用MyModel.MyTools.Orders.SumOrder
@模型订单
@{
ViewBag.Title=“查看发票”;
}
查看发票
@foreach(Model.OCI中的var prod)
{
@产品客户定位
foreach(prod.Details中的var orderItem)
{
@orderItem.Product
@orderItem.ItemQuantity
}
}
表中的第一行显示正确,这是一个城市的名称,但在下一行中,我得到:
System.Collections.Generic.List1[System.String]System.Collections.Generic.List1[System.Int32]
有人能给我解释一下为什么我不能以可读的格式返回列表,以及如何纠正这个问题吗
以下是我用来对ViewInvoice模型的列表进行分组的代码:
public SumOrder(List<orders_Cart> order)
{
// create list of order cart item
List<OrderCartItems> cartItems = new List<OrderCartItems>();
// convert orders to ocm
foreach(var item in order)
{
var newCartItem = new OrderCartItems();
try
{
newCartItem.Product = db.product_Product.FirstOrDefault(p =>
p.Id == item.ProductId).ProductDescription ?? "none";
}
catch (Exception)
{
newCartItem.Product = "none";
}
try
{
newCartItem.ClientForProduct = MyTool.OrdersFindClientLocation(
(int) item.ClientForOrdersId);
}
catch (Exception)
{
newCartItem.ClientForProduct = new object[3];
}
try
{
newCartItem.ProductSize = db.products_Size.FirstOrDefault(p => p.Id ==
item.ProductSizeId).ProductSizeCode ?? "none";
}
catch (Exception)
{
newCartItem.ProductSize = "none";
}
try
{
newCartItem.PackageType = db.packaging_PackageType.FirstOrDefault(p =>
p.Id == item.PackageTypeId).PackageTypeCode ?? "none";
}
catch (Exception)
{
newCartItem.PackageType = "none";
}
newCartItem.OrderDate = (DateTime) item.OrderDate;
newCartItem.DeliveryDate = (DateTime) item.DeliveryDate;
newCartItem.OrderNumber = (int) item.OrderNumber;
newCartItem.Price = (decimal) item.Price;
newCartItem.ClientLocation = MyTool.OrdersFindClientLocation(
(int) item.ClientForOrdersId, null);
newCartItem.ItemQuantity = (int) item.Quantity;
cartItems.Add(newCartItem);
}
// 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();
// set the OCI property
OCI = ordersGrouped;
};
公共订单(列表顺序)
{
//创建订单购物车项目列表
List cartItems=新列表();
//将订单转换为ocm
foreach(订单中的var项目)
{
var newCartItem=neworderCartItems();
尝试
{
newCartItem.Product=db.Product\u Product.FirstOrDefault(p=>
p、 Id==item.ProductId).ProductDescription???“无”;
}
捕获(例外)
{
newCartItem.Product=“无”;
}
尝试
{
newCartItem.ClientForProduct=MyTool.OrdersFindClientLocation(
(int)item.ClientForOrdersId);
}
捕获(例外)
{
newCartItem.ClientForProduct=新对象[3];
}
尝试
{
newCartItem.ProductSize=db.products\u Size.FirstOrDefault(p=>p.Id==
item.ProductSizeId).ProductSizeCode???“无”;
}
捕获(例外)
{
newCartItem.ProductSize=“无”;
}
尝试
{
newCartItem.PackageType=db.packaging\u PackageType.FirstOrDefault(p=>
p、 Id==item.PackageTypeId).PackageTypeCode???“无”;
}
捕获(例外)
{
newCartItem.PackageType=“无”;
}
newCartItem.OrderDate=(DateTime)item.OrderDate;
newCartItem.DeliveryDate=(DateTime)item.DeliveryDate;
newCartItem.OrderNumber=(int)item.OrderNumber;
newCartItem.Price=(十进制)item.Price;
newCartItem.ClientLocation=MyTool.OrdersFindClientLocation(
(int)item.ClientForOrdersId,null);
newCartItem.ItemQuantity=(int)item.Quantity;
cartItems.Add(newCartItem);
}
//根据位置对cartItems进行分组
List ordersGrouped=cartItems.GroupBy(c=>new
{c.ClientLocation})
.OrderBy(c=>c.Key.ClientLocation)。选择(s=>
新视图发票()
{
ClientLocation=s.Key.ClientLocation,
详细信息=新列表()
{
新建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();
//设置OCI属性
OCI=订单分组;
};
好的,我终于解决了我的问题。起初我对这个问题考虑过了。我简化了模型,并在视图中添加了一些简单的逻辑
以下是更新后的模型:
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; }
}
公共类视图发票
{
公共字符串ClientLocation{get;set;}
公共列表产品{get;set;}
公共列表ProductSize{get;set;}
公共列表PackageType{get;set;}
公共列表OrderDate{get;set;}
公共列表交付日期{get;set;}
公共列表顺序号{get;set;}
公开标价{get;set;}
公共列表项数量{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>