c# - 在 ASP.Net MVC View 中显示模型列表

标签 c# asp.net-mvc razor

这是我的模型:

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.List1[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/

相关文章:

c# - 如何在 C# 中返回引用?

c# - 如何使用 Linq "NOT IN"

c# - 如何异步更新 ASP.NET MVC 分部 View ?

asp.net-mvc - 在出现特定操作时使用 MVC 路由覆盖 Controller 名称

c# - Blazor (c#) DXDatagrid (devexpress) : Problem with Master Detail editor when replacing the default command buttons with icons

c# - 如何以最高性能复制对象

c# - 以尽可能小的努力从 StreamWriter 继承

c# - 是否可以部分部署到 ASP.Net MVC?

razor - 无法让 FontAwesome 在 Razor 中工作

asp.net-mvc - 类型 'System.Web.Mvc.MvcWebRazorHostFactory' 的表达式不能用于返回类型 'System.Web.WebPages.Razor.WebRazorHostFactory'