1

我正在使用 Microsoft ASP.NET MVC 4 版本 4.0.30.506.0 编写 MVC4,并且我在剃须刀页面中有一个 for 循环,它会生成一个愚蠢的运行时错误!

这是我写的代码:

@{
                    if (Model.IsAuction)
                    {
                        <li class="bc49">@Html.ActionLink("ABCD", "Search", "Home", new {city = "all", type = "auction", category = "all", keyword = "all", condition = "all", sort = "asc"})</li>
                    }
                    else
                    {
                        <li class="bc49">@Html.ActionLink("EFGH", "Search", "Home", new {city = "all", type = "product", category = "all", keyword = "all", condition = "all", sort = "asc"})</li>
                    }

                    int crumpsCount = 48;

                    for (int i = 0; i < Model.Categories.Count; i++)
                    {
                        className = "bc" + crumpsCount;

                        if (Model.IsAuction)
                        {
                            <li class="@className">@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "auction", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }
                        else
                        {
                            <li class="@className">@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "product", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }

                        crumpsCount--;
                    }
                }

这是 Visual Studio 2012 生成的错误:

产生错误!

错误发生在循环完成并且 i 的值等于 Model.Categories.Count 之后。似乎编译器试图访问 Model.Categories[Model.Categories.Count] 值!

有趣的事实是,当我用 foreach 循环替换这段代码时,或者即使我在 for 循环之后添加了一个 foreach 循环,代码也会正确执行并通过 for 循环!

这是有效的代码:

@{
                    if (Model.IsAuction)
                    {
                        <li class="bc49">@Html.ActionLink("ABCD", "Search", "Home", new {city = "all", type = "auction", category = "all", keyword = "all", condition = "all", sort = "asc"})</li>
                    }
                    else
                    {
                        <li class="bc49">@Html.ActionLink("EFGH", "Search", "Home", new {city = "all", type = "product", category = "all", keyword = "all", condition = "all", sort = "asc"})</li>
                    }

                    int crumpsCount = 48;

                    for (int i = 0; i < Model.Categories.Count; i++)
                    {
                        className = "bc" + crumpsCount;

                        if (Model.IsAuction)
                        {
                            <li class="@className">@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "auction", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }
                        else
                        {
                            <li class="@className">@Html.ActionLink(Model.Categories[i].Title, "Search", "Home", new {city = "all", type = "product", category = Model.Categories[i].Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }

                        crumpsCount--;
                    }

                    foreach (CategoryInfo categoryInfo in Model.Categories)
                    {
                        className = "bc" + crumpsCount;

                        if (Model.IsAuction)
                        {
                            <li class="@className">@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "auction", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }
                        else
                        {
                            <li class="@className">@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "product", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }

                        crumpsCount--;
                    }
                }

我刚刚分享了这段代码,看看这是否是一个错误,如果它是一个错误,它是否是已知的。

谢谢大家。:)

更新: 这太愚蠢了,现在代码也不适用于 foreach 循环!

在此处输入图像描述

foreach 循环内没有索引,但异常提到索引超出范围!

这是代码:

foreach (CategoryInfo categoryInfo in Model.Categories)
                    {
                        className = "bc" + crumpsCount;

                        if (Model.IsAuction)
                        {
                            <li class="@className">@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "auction", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }
                        else
                        {
                            <li class="@className">@Html.ActionLink(categoryInfo.Title, "Search", "Home", new {city = "all", type = "product", category = categoryInfo.Name, keyword = "all", condition = "all", sort = "asc"})</li>
                        }

                        crumpsCount--;
                    }

我还应该提到 Model.Categories 是一个 IList<> 对象。

4

1 回答 1

1

问题解决了。MVC 中没有我看到的错误,但是在 MVC/Razor 运行时中引用了错误的异常,它引用了确切错误位置上方的一行,让我认为框架的运行时中存在错误。真正的异常是关于一个从数据库返回为空的数组,而我编写的代码试图访问该数组的索引 0,因此我的代码中有一个超出范围的异常,但该异常引用了 10 行以上我写的确切访问代码,所以我认为这将是一个错误。感谢您的回复评论,并希望这将有助于将来的人。

于 2013-06-19T06:59:24.823 回答