如何迭代字典<string,list <object>&gt;

时间:2018-02-27 13:01:34

标签: c# .net asp.net-mvc dictionary razor

我有一个Dictionary<string,List<object>>,我希望迭代它,并希望绑定cshtml页面上的列表值。我得到的值为: image showing retrieve dictionary values

这是我的cshtml页面,我正在循环Dictionary<string,List<object>>

<div class="content">

    <div class="quote-slider full-bottom" data-snap-ignore="true">
        @foreach (var PriceList in Model.ProductPriceDicList)
        {
            <div>
                <br />
                <h4 style="font-weight:bold; color:red">@PriceList.Key</h4>
                <table  >
                 @foreach (var DicPriceList in Model.ProductPriceDicList.Values)
                  {                        
                    <tr>
                        <td style="padding:0px">
                            <table style="margin-bottom:0px; border:none"  >
                                <tr style="padding:0px">
                                    <td>
                                        @DicPriceList[Convert.ToInt32(PriceList.Key)].SubProductName
                                    </td>
                                    <td>
                                        @DicPriceList[Convert.ToInt32(PriceList.Key)].ProductCost
                                    </td>
                                    <td>
                                        <em class=" fa fa-eye"> </em>
                                    </td>
                                </tr>

                            </table>


                        </td>
                    </tr>

                 }
                </table>

            </div>
        }
    </div>

我的问题是如何遍历字典,我可以在其中获取标题为Key和值为List of object。 Thanku

1 个答案:

答案 0 :(得分:-3)

我写了一段时间,将目录结构输出到xml。您可以轻松修改此代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace SAveDirectoriesXml
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        const string FOLDER = @"c:\temp";
        static XmlWriter writer = null;
        static void Main(string[] args)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            writer = XmlWriter.Create(FILENAME, settings);
            writer.WriteStartDocument(true);

            DirectoryInfo info = new DirectoryInfo(FOLDER);
            WriteTree(info);

            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            Console.WriteLine("Enter Return");
            Console.ReadLine();

        }
        static long WriteTree(DirectoryInfo info)
        {
            long size = 0;
            writer.WriteStartElement("Folder");
            try
            {
                writer.WriteAttributeString("name", info.Name);
                writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
                writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
                writer.WriteAttributeString("date", info.LastWriteTime.ToString());


                foreach (DirectoryInfo childInfo in info.GetDirectories())
                {
                    size += WriteTree(childInfo);
                }

            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error", errorMsg);
            }

            FileInfo[] fileInfo = null;
            try
            {
                fileInfo = info.GetFiles();
            }
            catch (Exception ex)
            {
                string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
                Console.WriteLine(errorMsg);
                writer.WriteElementString("Error",errorMsg);
            }

            if (fileInfo != null)
            {
                foreach (FileInfo finfo in fileInfo)
                {
                    try
                    {
                        writer.WriteStartElement("File");
                        writer.WriteAttributeString("name", finfo.Name);
                        writer.WriteAttributeString("size", finfo.Length.ToString());
                        writer.WriteAttributeString("date", info.LastWriteTime.ToString());
                        writer.WriteEndElement();
                        size += finfo.Length;
                    }
                    catch (Exception ex)
                    {
                        string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
                        Console.WriteLine(errorMsg);
                        writer.WriteElementString("Error", errorMsg);
                    }
                }
            }

            writer.WriteElementString("size", size.ToString());
            writer.WriteEndElement();
            return size;

        }
    }
}