我正在使用 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<> 对象。