-2

I have a Dictionary<string,List<object>> and i want iterate though it and want to bind the list values on cshtml page.And i am getting the values as : image showing retrieve dictionary values

This is my cshtml page where i am looping though 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>

my question is how to iterate through the dictionary, where i can get header as Key and value as List of object. Thanku

  • Did you check this out? [What is the best way to iterate over a Dictionary in C#?](https://stackoverflow.com/questions/141088/what-is-the-best-way-to-iterate-over-a-dictionary-in-c?rq=1) – Rafalon Feb 27 '18 at 13:03
  • `@foreach (var DicPriceList in Model.ProductPriceDicList.Values)` should be `@foreach (var DicPriceList in Model.ProductPriceDicList)` and `DicPriceList[Convert.ToInt32(PriceList.Key)]` should be `DicPriceList.Key` or `DicPriceList.Value` depending on whether you want the key or value. – mjwills Feb 27 '18 at 13:05
  • @mjwills... DicPriceList.Value gives the full object values but i want something like DicPriceList.Value.ProductName – sumit.spider Feb 27 '18 at 13:15
  • @mjwills ..thanku it worked..instead of Value i was using Values. – sumit.spider Feb 28 '18 at 06:33

1 Answers1

-3

I wrote a while back a project that outputs the directory structure to xml. You can easily modify this code for you purpose :

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;

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 2
    I'd upvote if it wasn't for the extreme amount of irrelevant code – Camilo Terevinto Feb 27 '18 at 13:10
  • The is nothing irrelevant in the code. The program will run to completion even if the user doesn't have the privilege to access the folder. It is a great program especially for people who never used recursion. – jdweng Feb 27 '18 at 13:41
  • And how does this code demonstrate how to iterate a dictionary? I don't even see a dictionary in there – Camilo Terevinto Feb 27 '18 at 13:42
  • I'm not sure if the dictionary is the best choice of a solution because of the hierachy of the folders. The real issue is : bind the list values on cshtml page. A recursive method is needed and posted code does not have recursion to keep the folder structure. – jdweng Feb 27 '18 at 13:45
  • 2
    And where do you read that the OP cares about folders? Are you sure you posted the answer to the correct question? – Camilo Terevinto Feb 27 '18 at 13:47