2017-10-28 9 views
1

私はMVC、JSON、Razor、実際には.NETの新機能です。MVCとRazorで入れ子になったJSON要素にアクセスする

私はCosmosDB demo from Microsoftで問題なく処理しています。

次に、このコードをベースとして使用して、ネストされたJSON要素をHTMLテーブルに取得し始めました。私はネストされた要素の最初のレベルでこれを成功裏に実行しましたが、2番目のレベルでは苦労しました。

これは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> 

私は成功したエントリ(日、時間、場所)で要素を取得することができますが、私はしようとしたら、 Details(Textからネストされたもの)にアクセスしてください。私はそれを参照することはできません。私はエラーを取得しているため

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

は「エントリは[]」「テキスト」と拡張子なしのメソッド「テキスト」の定義が含まれていません:

問題は、このラインであります'Entry []'型の最初の引数を受け入れることができます(usingディレクティブまたはアセンブリ参照がありませんか?)。

私はエントリ内のクラス詳細をネストしようとしましたが、同じ問題があるようです。私はそれがモデルの定義に関係していると仮定しています。

感謝の言葉をいただきました!

答えて

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> 
} 
+0

パーフェクトにする必要があります。ありがとう! – GPT

関連する問題