C# 在 ASP.Net MVC 视图中显示模型列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12888484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:50:07  来源:igfitidea点击:

Display list from model in ASP.Net MVC view

c#asp.net-mvcrazor

提问by user1206480

Here is my model:

这是我的模型:

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; }
    }
}

I am trying to display this model in a table within my razor view. Here is that code:

我正在尝试在我的剃刀视图中的表格中显示此模型。这是代码:

@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>

The first row in the table displays correctly, which is the name of a city, but in the next row I get this:

表中的第一行显示正确,这是一个城市的名称,但在下一行中我得到了这个:

System.Collections.Generic.List1[System.String] System.Collections.Generic.List1[System.Int32]

System.Collections.Generic.List 1[System.String] System.Collections.Generic.List1[System.Int32]

Can someone explain to me why I can not get the list returned in a readable format, and how to correct this problem?

有人可以向我解释为什么我无法以可读格式返回列表,以及如何解决此问题?

Here is the code I used to group the list for the ViewInvoice model:

这是我用来对 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();

采纳答案by user1206480

Ok, I finally solved my problem. I initially over-thought the problem. I simplified my model, and added some simple logic to my view.

好的,我终于解决了我的问题。一开始我把问题想得太多了。我简化了我的模型,并在我的视图中添加了一些简单的逻辑。

Here is the updated Model:

这是更新后的模型:

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; }
}

the updated code used to group the list for the Model:

用于对模型列表进行分组的更新代码:

// 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();

and the updated 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>    

This solution clearly allows me to iterate through the model reproducing any required rows or cells along the way. Played Russian roulette for the past two days over this problem. Hope this saves some time for others facing this issue.

这个解决方案显然允许我迭代模型,在此过程中重现任何所需的行或单元格。过去两天为了这个问题玩了俄罗斯轮盘赌。希望这可以为面临此问题的其他人节省一些时间。

回答by Alin Cosma

Because a List of strings is not the same as a string. What you are seeing is what the ToString() method of the List class is returning, the default value.

因为字符串列表与字符串不同。您所看到的是 List 类的 ToString() 方法返回的内容,即默认值。

To get the elements from the list in a comma separated display use:

要以逗号分隔的显示方式从列表中获取元素,请使用:

string.Join(",", orderItem.Product.ToArray())
string.Join(",", orderItem.ItemQuantity.ToArray())

Whenever you want to print something that object's ToString() method will be called. This ToString() method defined on the List<> class cannot know how you would like your data to be displayed, seeing how a list can caontain more than one String or int or Date. You may want to add a <p> in front of every value and a </p> after every value for example.

每当你想打印一些东西时,对象的 ToString() 方法就会被调用。这个在 List<> 类上定义的 ToString() 方法无法知道您希望如何显示您的数据,看看一个列表如何可以包含多个字符串或整数或日期。例如,您可能希望在每个值之前添加一个 <p> 并在每个值之后添加一个 </p>。

The first row is showing up correctly because ClientLocation is a simple type. For some more info on simple types see simple types

第一行显示正确,因为 ClientLocation 是一个简单类型。有关简单类型的更多信息,请参阅简单类型

回答by Andrew Stakhov

Your model object doesn't make any sense. Why is each of your properties such as Price and Quantity a collection? A single order line item object will have just a single value for each of the properties, not a collection. Your invoice object already accounts for multiple Details object, but a single detail object should not have it's properties as lists. Remove the List<> from all your properties and it should solve it.

您的模型对象没有任何意义。为什么您的每个属性(例如价格和数量)都是一个集合?单个订单行项目对象的每个属性只有一个值,而不是集合。您的发票对象已经考虑了多个 Details 对象,但单个明细对象不应将其属性作为列表。从所有属性中删除 List<> ,它应该可以解决它。