在MVC和Razor中访问嵌套的JSON元素

时间:2017-10-28 09:57:55

标签: json asp.net-mvc azure razor

我对MVC,JSON,Razor以及.Net都很陌生 - 所以对我来说很容易。

我一直在研究CosmosDB demo from Microsoft而没有任何问题。

然后我开始使用代码作为基础来检索嵌套的JSON元素到HTML表中。我已经设法成功地为第一级嵌套元素做了这个,但是已经在第二级进行了努力。

这是一个示例JSON文档。

{
"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..."}
         ] 
     }
 }
 ]
 }

这是模型:

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

}

观点是:

@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>

我可以成功检索条目中的元素(日期,时间,位置)但是一旦我尝试访问详细信息(嵌套文本)中的元素,我似乎无法引用它。

问题是这一行:

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

我得到了错误

  

&#39;入口[]&#39;不包含&#39;文字&#39;的定义没有扩展方法&#39; Text&#39;接受第一个类型为&#39; Entry []&#39;可以找到(你错过了使用指令或程序集引用吗?)。

我尝试在Entry中嵌套类详细信息,但似乎有同样的问题。我假设它与模型的定义有关。

感激地收到任何建议!

1 个答案:

答案 0 :(得分:1)

Model.Entries的类型为Entry[]。每个Entry都有Text个属性。但是,您尝试从阵列本身访问属性Text。你的代码应该是:

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