Accessing nested JSON elements in MVC and Razor

徘徊边缘 提交于 2021-01-28 06:53:46

问题


I'm very new to MVC, JSON, Razor and indeed .Net - so go easy on me.

I have been working through the CosmosDB demo from Microsoft with no problems.

I then started using the code as a base to retrieve nested JSON elements into a HTML table. I have managed to do this successfully for the first level of nested elements but have struggled with the second level.

This a sample JSON document.

{
"title": "War Diary for 2nd Battalion Scots Guards September 1943",
"date": "September 1943",
"unit": "2nd Battalion Scots Guards",
"brigade": "201 Guards Brigade",
"division": "56 Infantry Division",
"entries": [
{
  "day": "1st",
  "location": "Tripoli",
  "time": "",
  "text": [
    {
      "p": "The L.S.T. party march to the Race Course prior to embarkation...."
    }
  ]
},
{
  "day": "2nd",
  "location": "",
  "time": "",
  "text": [
    {
      "p": "A two hours march brings the L.S.T. party to the quay..."}
  ]
},
{
  "day": "3rd",
  "location": "",
  "time": "",
  "text": [
    {
      "p": "The \"fighting four hundred\"; or the two L.C.I's parties march..." },
      "p": "The L.C.I. parties embark. Last minute-farewells. Convoy sails after dark..."}
         ] 
     }
 }
 ]
 }

This is the model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;

namespace todo.Models
{
public class WarDiary
    {
       [JsonProperty(PropertyName = "id")]
       public string Id { get; set; }

       [JsonProperty(PropertyName = "title")]
       public string Title { get; set; }

        [JsonProperty(PropertyName = "date")]
        public string Date { get; set; }

        [JsonProperty(PropertyName = "unit")]
        public string Unit { get; set; }

        [JsonProperty(PropertyName = "brigade")]
        public string Brigade { get; set; }

        [JsonProperty(PropertyName = "division")]
        public string Division { get; set; }

        [JsonProperty(PropertyName = "entries")]
        public Entry [] Entries { get; set; }
    }

    public class Entry
    {
        [JsonProperty(PropertyName = "text")]
        public Details[] Text { get; set; }

        [JsonProperty(PropertyName = "day")]
        public string Day { get; set; }

        [JsonProperty(PropertyName = "time")]
        public string Time { get; set; }

        [JsonProperty(PropertyName = "location")]
        public string Location { get; set; }
    }

    public class Details
    {
        [JsonProperty(PropertyName = "p")]
        public string P { get; set; }
    }

}

The view is:

@model todo.Models.WarDiary

@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details</h2>
<div>
<h4>WarDiary</h4>
<hr />

<table class="table">
    <tr>
        <th>
            Location
        </th>
        <th>
            Day
        </th>
        <th>
            Time
        </th>
        <th>
            Summary
        </th>
    </tr>
    @foreach (var entry in Model.Entries)
    {
        <tr>
            <td>
                @entry.Location
            </td>
            <td>
                @entry.Day
            </td>
            <td>
                @entry.Time
            </td>
            <td>
                @foreach (var p in Model.Entries.Text)
                {
                    <p>
                        @p.P
                    </p>
                }
            </td>
        </tr>
    }
</table>

I can successfully retrieve the elements in Entry (day, time, location) but once I try to access those in Details (nested off Text) I can't seem to reference it.

The issue is this line:

@foreach (var p in Model.Entries.Text)

for which I get the error

'Entry[]' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'Entry[]' could be found (are you missing a using directive or an assembly reference?).

I have tried nesting the class Details within Entry but seem to have the same issue. I'm assuming it is related to the definition of the model.

Any advice gratefully received!


回答1:


Model.Entries is of type Entry[]. Each Entry has a Text property. But, you are trying to access the property Text from the array itself. Your code should be:

@foreach (var entry in Model.Entries)
{
    <tr>
        <td>
            @entry.Location
        </td>
        <td>
            @entry.Day
        </td>
        <td>
            @entry.Time
        </td>
        <td>
            @foreach (var p in entry.Text)
            {
                <p>
                    @p.P
                </p>
            }
        </td>
    </tr>
}


来源:https://stackoverflow.com/questions/46988439/accessing-nested-json-elements-in-mvc-and-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!