简体   繁体   中英

Using dictionary to display values in VIEW

Using Orchard 1.6 MVC I wanted to display a list of 'suppliers' on screen. For every supplier, display a list of their products, and the quantity remaining.

Supplier 1
product 101
product 102

supplier 2
product 103
product 104
product 105

etc...

My ViewModel

public class SuppliersOrderVM
    {
        public IEnumerable<SupplierInfo> SupplierInformation = new List<SupplierInfo>();
        public Dictionary<int, QBSupplierRecord> Suppliers { get; set; }
        public Dictionary<int, QBProductRecord> Products { get; set; }

        public SuppliersOrderVM(IEnumerable<SupplierInfo> supplierInformation, Dictionary<int, QBSupplierRecord> suppliers, Dictionary<int, QBProductRecord> products)
        {
            SupplierInformation = supplierInformation;
            Suppliers = suppliers;
            Products = products;
        }

        public class SupplierInfo
        {
            public int SupplierId { get; set; }
            public bool SendEmail { get; set; }

            public List<ProductInfo> Products = new List<ProductInfo>();
        }

        public class ProductInfo
        {
            public int ProductId { get; set; }
            public int Quantity { get; set; }
        }
    }

So in the view Ive used the dictionary to output the supplier name. But I am having trouble outputting the products for that supplier. My 2nd foreach loop may be wrong and my syntax is incorrect for displaying product. Please help me amend it

 @{
foreach (var supplier in Model.SupplierInformation)
{
            <div class="editor-label">@Html.Label("SupplierName")</div>
            <div class="editor-label">@Model.Suppliers[supplier.SupplierId].CompanyName</div>

           @* <div class="editor-label">@Html.HiddenFor(s => s.SupplierInfor[0].</div>*@

    foreach (var product in Model.SupplierInformation)
    {
            //create table and output product code & product Quantity
<div class="editor-label">@Model.Products[product.Products].ProductCode</div>

It is telling me [product.Products] has some invalid arguments.

Correct me if I am wrong, but as per my understanding you are trying to display

  1. ProductCode which is in the QBProductRecord class and this class is being used as part of dictionary (in which the key being used is the ProductId ) in your model class SuppliersOrderVM .
  2. Quantity which is a property in the class Product .

Then you should use the following code. The error in you code is that you are trying to use product.Products as an index which should be an integer but it's not.

@{
foreach (var supplier in Model.SupplierInformation)
{
            <div class="editor-label">@Html.Label("SupplierName")</div>
            <div class="editor-label">@Model.Suppliers[supplier.SupplierId].CompanyName</div>

           @* <div class="editor-label">@Html.HiddenFor(s => s.SupplierInfor[0].</div>*@

    foreach (var product in supplier.Products)
    {
            //create table and output product code & product Quantity
<div class="editor-label">@Model.Products[product.ProductId].ProductCode</div>
           //display quantity
<div class="editor-label">product.Quantity</div>

What is this line meant to do?:

Model.Products[product.Products]

If I'm following your model hierarchy correctly, Model is an instance of SuppliersOrderVM . Which means that Model.Products is an instance of Dictionary<int, QBProductRecord> . In order to access an element from that Dictionary you need to index it with an int . However, product.Products isn't an int , it's a List<ProductInfo> .

Did you mean to add another loop for the List<ProductInfo> , in order to access the ProductId property on each element therein? Something like this?:

foreach (var productInfo in product.Products)
    <div class="editor-label">@Model.Products[productInfo.ProductId].ProductCode</div>

It's difficult to tell exactly what you're trying to do. The model hierarchy is a little awkward with so many things called "product."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM