2013-06-28 31 views
0

我有一個名爲Video的模型,它有一系列附加的反饋評論和反饋評論回覆。我在View中創建了以下循環,以返回反饋評論和相關反饋評論回覆的詳細信息,但儘管Model.SelectedScreecast.Feedbacks的計數爲3,但我可以看到有3個反饋記錄迭代進入循環第四次,我收到一個對象引用錯誤。我的代碼如下:剃刀網頁中模型屬性的返回值返回不正確的值

@foreach (var comment in Model.SelectedScreencast.Feedbacks) 
     { 
      <li class="comment"> 
      <div class="avatar"> <img src="@Url.Content("~/Images/icons/avatar.png")" width="50" height="50" alt="Avatar" /> </div> 
      <div class="comment-meta"> 
       <h5 class="author"><a href="#">John Doe</a> - <a href="#" class="comment-reply-link">Reply</a></h5> 
       <p class="date">January 06, 2011</p> 
      </div> 
      <div class="comment-body"> 
       <p>@comment.FeedbackString</p> 
      </div> 
      <ul class="children"> 

      @foreach (var reply in @comment.FeedbackReplys) 
      { 
       <li class="comment"> 
       <div class="avatar"> <img src="@Url.Content("~/Images/icons/avatar.png")" width="50" height="50" alt="Avatar" /> </div> 
       <div class="comment-meta"> 
        <h5 class="author"><a href="#">John Doe</a> - <a href="#" class="comment-reply-link">Reply</a></h5> 
        <p class="date">January 06, 2011</p> 
       </div> 
       <div class="comment-body"> 
        <p>reply string</p> 
       </div> 
       </li> 
      }      
      </ul> 
      </li> 
     }   

我可以使用下面的代碼時,雖然這是沒有頁面的必要的造型,以返回預期的字符串。

@foreach (var comment in Model.SelectedScreencast.Feedbacks) 
     { 
      <li>@comment.FeedbackString</li> 

      foreach (var reply in @comment.FeedbackReplys) 
      { 
       <li>@reply.FeedbackReplyString</li> 
      }  
} 

什麼會導致此來評估當它正在通過調試檢查設定並使用上面的代碼塊時不設置到對象的實例對象。任何幫助將不勝感激。

+1

你確定你沒有設置第四條記錄爲空嗎?因爲它聽起來像你的foreach計數4記錄,而不是3. 嘗試檢查調試器中的視圖,看看「Model.SelectedScreencast.Feedbacks」是什麼內容。 –

+0

我檢查了調試,它返回[0] [1] [2]反饋總計數被設置爲3.我不明白爲什麼它讀這個爲4 – Jay

+0

'comment.FeedbackReplys'也一樣? –

回答

1

你的foreach有問題。

@foreach (var reply in @comment.FeedbackReplys)

應該是:@foreach (var reply in comment.FeedbackReplys)

甚至:foreach (var reply in comment.FeedbackReplys)

剃刀語法啓動帶有@字符的代碼塊,並且不需要顯式關閉代碼塊。所以第二個@是多餘的,所以可能第二個是@

不知道是否會,但希望這解決了您的問題!

+0

您好Wouter感謝您的努力和建議,我會銘記這對未來,但這不是問題的起因不幸 – Jay

+0

您好Wouter,我有應用您的建議並在底部使用更新問題的簡單格式,我至少能夠返回我在哪裏知道的值。但是,當應用html樣式時,它返回對象引用錯誤。任何想法可能會導致什麼? – Jay

+0

嘗試使用'@:'直接將輸入的字符串輸入到html中。 像這樣:'@:

hello

'。 而且您的更新還需要: 'foreach(var reply in comment.FeedbackReplys)'而不是'foreach(var reply in @ comment.FeedbackReplys)' –