asp.net MVC - 只在Razor视图中迭代字典时奇怪的索引超出范围

时间:2017-05-18 20:03:57

标签: c# asp.net-mvc razor

在迭代浏览我的viewmodel的字典属性时,我得到一个奇怪的“索引超出范围”异常,该属性只是int Key和对象列表为Value。这很奇怪,因为只有在Razor视图中迭代字典时才会发生异常。只是为了我自己的理智,我在我的控制器中运行完全相同的迭代过程没有任何问题。这是代码:

        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
=========================
+++++++++++++++++++++++++++++

然而,当我在剃刀视图中使用几乎相同的结构来构建表时,我得到一个'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>

}

在这种情况下,只要执行第3次到达内部for循环的顶部,就会发生异常。这是关键[2](上面的'第2阶段')。当列表的长度(字典值)大于或等于它所映射到的字典键的索引时,我能够成功地呈现视图。例如,在上面的调试输出中,“阶段2”位于第3个键索引[2],但其值列表只有Count 2,因此发生异常。奇怪的是,如果我使该值列表的计数为3或更多,一切正常。我已经在这一段时间里摸不着头脑了,所以对于为什么会发生这种情况的任何见解都将非常感激。

1 个答案:

答案 0 :(得分:1)

Welp,我感觉非常愚蠢。

此按钮的索引上的错字导致问题

<button class="btn btn-primary" type="button" onclick="addGoalToTemplate(@Model.PhaseTemplateDict.Values.ElementAt(i)[i].ProgramLevel)">Add Goal</button>

应该是:

<button class="btn btn-primary" type="button" onclick="addGoalToTemplate(@Model.PhaseTemplateDict.Values.ElementAt(i)[0].ProgramLevel)">Add Goal</button>

我刚刚将'ElementAt'的索引从(i)[i]更改为(i)[0]。