简体   繁体   中英

How to access Dictionary Model Property value from View - ASP.Net MVC

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

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

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