我有一個名爲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>
}
}
什麼會導致此來評估當它正在通過調試檢查設定並使用上面的代碼塊時不設置到對象的實例對象。任何幫助將不勝感激。
你確定你沒有設置第四條記錄爲空嗎?因爲它聽起來像你的foreach計數4記錄,而不是3. 嘗試檢查調試器中的視圖,看看「Model.SelectedScreencast.Feedbacks」是什麼內容。 –
我檢查了調試,它返回[0] [1] [2]反饋總計數被設置爲3.我不明白爲什麼它讀這個爲4 – Jay
'comment.FeedbackReplys'也一樣? –