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<> オブジェクトであることにも言及する必要があります。