繁体   English   中英

asp.net MVC-仅在Razor视图中迭代字典时,奇异索引超出范围

[英]asp.net MVC - Bizarre Index out of Range when iterating Dictionary in Razor View ONLY

当我通过视图模型的字典属性进行迭代时,遇到一个奇怪的“索引超出范围”异常,该属性只是int Key和Object List as 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>

}

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

韦尔普,我感觉很傻。

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

<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]。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM