我得到通過我的視圖模型的字典屬性進行遍歷時,一個奇怪的「索引超出範圍」例外迭代字典時,這是簡單的INT重點和對象的價值列表。這很奇怪,因爲只有在Razor視圖中迭代字典時纔會發生異常。只是爲了我自己的理智,我在控制器中運行了完全相同的迭代過程,沒有任何問題。這裏是代碼爲:asp.net MVC - 奇異指數超出範圍的Razor視圖ONLY
Debug.WriteLine("+++++++++++++++++++++++++++++");
for (int i = 0; i < viewModel.PhaseTemplateDict.Keys.Count(); i++)
{
Debug.WriteLine("Phase Name: " + viewModel.PhaseTemplateDict.Values.ElementAt(i)[0].ProgramLevelName);
Debug.WriteLine("");
for (int j = 0; j < viewModel.PhaseTemplateDict.Values.ElementAt(i).Count(); j++)
{
Debug.WriteLine("Goal Name: " + viewModel.PhaseTemplateDict.Values.ElementAt(i)[j].GoalDescription);
}
Debug.WriteLine("=========================");
}
Debug.WriteLine("+++++++++++++++++++++++++++++");
這產生所需的輸出,沒有例外。
+++++++++++++++++++++++++++++
Phase Name: Orientation
Goal Name: Provider
Goal Name: UAs
Goal Name: P.O. Meeting
Goal Name: Provider Assessment
Goal Name: Court
=========================
Phase Name: Phase 1
Goal Name: School/Work
Goal Name: Treatment
Goal Name: Curfew Checks
Goal Name: PO Meetings
Goal Name: UAs
Goal Name: Court
=========================
Phase Name: Phase 2
Goal Name: test1
Goal Name: test2
=========================
Phase Name: Phase 3
Goal Name: test1
Goal Name: test2
Goal Name: test3
Goal Name: test4
=========================
+++++++++++++++++++++++++++++
然而,當我使用幾乎相同的結構,我的Razor視圖來建一個表,我得到一個「System.ArgumentOutOfRangeException - 索引超出範圍。必須是非負值且小於集合的大小'。查看:
@model MyApp.ViewModels.PhaseTemplatesViewModel
@using (Html.BeginForm("MyAction", "MyController", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="row">
<div class="col-md-offset-3 col-md-5">
@{ int index = 0;}
@for (int i = 0; i < Model.PhaseTemplateDict.Keys.Count(); i++)
{
<div class="row">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">@Model.PhaseTemplateDict.Values.ElementAt(i)[0].ProgramLevelName</h3>
</div>
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Description</th>
<th>Points Required</th>
<th>Display Order</th>
</tr>
</thead>
<tbody>
@for (int j = 0; j < @Model.PhaseTemplateDict.Values.ElementAt(i).Count(); j++)
{
@Html.HiddenFor(mdlItem => @Model.PhaseTemplateDict.Values.ElementAt(i)[j].PointMatrixTemplateID, new { Name = "[" + index + "]." + "PointMatrixTemplateID" })
@Html.HiddenFor(mdlItem => @Model.PhaseTemplateDict.Values.ElementAt(i)[j].ProgramLevel, new { Name = "[" + index + "]." + "ProgramLevel" })
<tr>
<td>
@Html.TextBoxFor(mdlItem => @Model.PhaseTemplateDict.Values.ElementAt(i)[j].GoalDescription, new { @class = "form-control", Name = "[" + index + "]." + "GoalDescription" })
</td>
<td>
@Html.TextBoxFor(mdlItem => @Model.PhaseTemplateDict.Values.ElementAt(i)[j].PointsRequired, new { @class = "form-control text-center", style = "width: 40px; padding: 0px", type = "number", Name = "[" + index + "]." + "PointsRequired" })
</td>
<td>
@Html.TextBoxFor(mdlItem => @Model.PhaseTemplateDict.Values.ElementAt(i)[j].DisplayOrder, new { @class = "form-control text-center", style = "width: 40px; padding: 0px", type = "number", Name = "[" + index + "]." + "DisplayOrder" })
</td>
</tr>
index++;
}
</tbody>
</table>
</div>
<div class="panel-footer">
<button class="btn btn-primary" type="button" onclick="addGoalToTemplate(@Model.PhaseTemplateDict.Values.ElementAt(i)[i].ProgramLevel)">Add Goal</button>
<button class="btn btn-success" type="submit">Save</button>
</div>
</div>
</div>
}
</div>
</div>
}
在這種情況下,只要執行次數達到第三次內循環的頂部,就會發生異常。這是關鍵[2](上面的'第二階段')。當列表的長度(字典值)大於或等於映射到的字典鍵的索引時,我已能夠成功地呈現視圖。例如,在上面的調試輸出,「2階段」是在第3密鑰索引[2],但其值列表只具有2的計數,所以發生異常。奇怪的是,如果我讓價值清單有3或更多的計數,一切工作正常。我一直在抓我的頭在這個一期一會,所以任何見解,爲什麼發生這種情況,將不勝感激。
爲什麼要把它作爲一個字典,如果你打算把它當作一個數組呢?爲了方便迭代我一般使用類似'的foreach(在dict.Keys變種K)'的字典。 – nurdyguy