-1

I want to get value of Dictionary from View. In the view, I have a main/first foreach loop that retrieve data from Model, and inside the main/first loop, I need to retrieve the ListAttribute value according to the first loop by Id.

**//Code in the View - First loop to retrieve data from Model**
@model IEnumerable<ABC.Web.Models.Room.Housing>
foreach (var item in Model.OrderBy(x => x.Id))
{
    <div class="col-xs-18 col-sm-4">
        <div class="thumbnail">

    *...(remainer of the code)*

    //Here to insert second loop to retrieve *ListAttribute*
}


//Code in the Model 
namespace ABC.Web.Models.Room
{    
    public class Housing
    {
        public string[] FloorPlan { get; set; }
        public Dictionary<string, ListAttribute> ListAttributes { get; set;}
        public string Latitude { get; set; }
    }

    public partial class ListAttribute
    {
        public string name { get; set; }
        public string icon { get; set; }
        public string value { get; set; }
    }
}
Chloe
  • 73
  • 9

1 Answers1

0

If item is an object of type Housing, then you can do this:

foreach (KeyValuePair<string, ListAttribute> listItem in item.ListAttributes)
{
    // Then you will have...
    // listItem.Key -> string
    // listItem.Value -> ListAttribute
}

Source

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • It doesn't work me. It's displaying the key value for all the **Id** from the top **foreach (var item in Model.OrderBy(x => x.Id))** I want to display dictionary value per Id. – Chloe Oct 23 '19 at 12:45
  • @Chloe must be something wrong with your Model then.. if an `item` is a different iteration from your Model, and then you display data for that `item`, it should be also different. – DontVoteMeDown Oct 23 '19 at 13:46