How to bind data from List<Dictionary<string, object>> to cshtml page RRS feed

  • Question

  • User-353733005 posted

    Hi, I've get data from API and store in List<Dictionary<string, object>>. But I can't bind this data in *.cshtml page. Can anyone help me how I can bind data from List<Dictionary<string, object>> to html table in *.cshtml page.

    Here is my API code:

    var contentString = await result.Content.ReadAsStringAsync();
    var values = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(contentString);

    can't extract data from List<Dictionary<string, object>>. Please anyone help me to extract data from this list. I'm new in asp.net core.

    Thanks in advance.

    Friday, August 14, 2020 7:28 PM

All replies

  • User-474980206 posted

    assuming you passed the list as the Model:

    @foreach (var dict in Model)
    	@foreach(var item in dict)
    	    <td>@item.Key </td><td>@item.Value.ToString()</td>

    note: if the dictionary object is not a scaler, then to string will just be the class name. No sure what you wanted to do. you could reflect t front the properties, but then you just might as well display the json string.

    Friday, August 14, 2020 7:51 PM
  • User-353733005 posted

    Thank you.

    when the Dictionary object is string I can get the data easily but If the dictionary have the object then how to extract that object value. Like:

    Dictionary<string, object> dict = new Dictionary<string, object>();

    [accountsHeads , { [ { "headCode": "AB", "headName": "AB" } ] } ]

    How I can extract this value for this (key, value) pair.

    Saturday, August 15, 2020 4:15 AM
  • User-474980206 posted

    You would need to use reflection to get the values, or if you know the possible data types a switch statement on type. If you just want a generic print of Json data, you could just use a json reader which will do all the parsing.

    Saturday, August 15, 2020 3:29 PM
  • User-17257777 posted

    Hi Shibly,

    If the object is dynamic, I think you can extract the data from dictionary in js:


     public IActionResult Test()
                var list = new List<Dictionary<string, object>>();
                    new Dictionary<string, object> {{ "accountsHeads", new { headCode="AB",headName="AB"} }
                    new Dictionary<string, object> {{ "accountsBodys", new { BodyCode="CD",BodyName="CD"} }
                return View(list); 


    @model List<Dictionary<string, object>>
        <table id="contents">
    @section scripts{
        $(document).ready(function () {
            var json = @Html.Raw(Json.Serialize(Model));
            for (var i = 0; i < json.length; i++) {
                var key = Object.keys(json[i]);
                var valuename = Object.keys(json[i][key[0]])
                var htmlStr = "<tr>";
                for (var j = 0; j < valuename.length; j++) {
                    htmlStr += "<td>" + json[i][key[0]][valuename[j]] + "</td>";
                htmlStr += "</tr>";


    Monday, August 17, 2020 9:13 AM