对于mvc中的文本框的循环

时间:2016-06-22 20:15:52

标签: c# asp.net-mvc for-loop textbox

我有一个for循环设置目前有效。但是,当我尝试返回到它出现的error页面时,会出现问题。 我试图让它最初提出两个他们需要填写的引用,如果他们想为这个页面添加最多两个额外的引用,总共4个人。这是我目前正在使用的代码。 这是页面视图:

    <div id="reference-list">
        @for (int i = 0; i < Model.Order.References.Count; i++)
        {
            <div class="reference nomination-person" id="reference-@i">

                    <h3>Reference @(i+1)</h3>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].Prefix)
                        @Html.DropDownListFor(m => m.Order.References[i].Prefix, new SelectList(prefixes))
                    </div>

                    <div class="person-attribute">

                        @Html.LabelFor(m => m.Order.References[i].FirstName)
                        @Html.TextBoxFor(m => m.Order.References[i].FirstName)
                        @Html.ValidationMessageFor(m => m.Order.References[i].FirstName)
                    </div>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].MiddleName)
                        @Html.TextBoxFor(m => m.Order.References[i].MiddleName)
                    </div>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].LastName)
                        @Html.TextBoxFor(m => m.Order.References[i].LastName)
                        @Html.ValidationMessageFor(m => m.Order.References[i].LastName)
                    </div>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].Suffix)
                        @Html.DropDownListFor(m => m.Order.References[i].Suffix, new SelectList(suffixes))
                    </div>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].Company)
                        @Html.TextBoxFor(m => m.Order.References[i].Company)
                    </div>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].EmailAddress)
                        @Html.TextBoxFor(m => m.Order.References[i].EmailAddress)
                        @Html.ValidationMessageFor(m => m.Order.References[i].EmailAddress)
                    </div>

                    <div class="person-attribute">
                        @Html.LabelFor(m => m.Order.References[i].Telephone)
                        @Html.TextBoxFor(m => m.Order.References[i].Telephone)
                        @Html.ValidationMessageFor(m => m.Order.References[i].Telephone)
                    </div>

            </div>
        }
    </div>
    <input type="hidden" name="n-references" id="n-references" value="0" />


    <div id="add-reference">Add Reference</div>

控制器

    private OrderData GetReferencesData(OrderData data, HttpRequestBase request)
    {
         data.References = new List<Person>();
        /* Since there can be many references, iterate along all and create individual 
         * persona objects for each */

        for (int i = 0; i <= request["n-references"].ToInt(); i++)
        {
            data.References.Add(new Person
            {
                Prefix = request["Order.Prefix-" + i] ?? "",
                Suffix = request["Order.Suffix-" + i] ?? "",
                FirstName = request["Order.FirstName-" + i] ?? "",
                MiddleName = request["Order.MiddleName-" + i] ?? "",
                LastName = request["Order.LastName-" + i] ?? "",
                Company = request["Order.Company-" + i] ?? "",
                Telephone = request["Order.Telephone-" + i] ?? "",
                EmailAddress = request["Order.EmailAddress-" + i] ?? ""
            });
        }

        return data;
    }

虽然在模型中它只是         public List<Person> References;

1 个答案:

答案 0 :(得分:0)

您的m.order.references中包含的元素少于4个。所以当你在索引上点击3或4时,它会超出Reference数组边界并给你一个错误。在尝试访问阵列中的斑点之前,您应该检查m.order.references的长度

<强> 修改 m.order.references是一个列表/数组(为了我们的目的,它们是可以互换的),这个对象将包含有限数量的项目,例如我们说它有3个项目。当您遍历列表时,如果您尝试访问m.order.references[4]的现场项目,则会出现超出范围的异常。这是因为位置4中没有项目。您需要做的是

for(int i = 0; i <= m.order.references.count; i++)如果你想让他们只能有4个引用你需要改变你的架构以拥有一个新的引用数组和一个现有的引用数组(或者某些东西,因为你当前的设计不支持此)