在Blazor组件中使用for循环获取ArgumentOutOfRangeException [英] Getting ArgumentOutOfRangeException using for loop in Blazor component

查看:315
本文介绍了在Blazor组件中使用for循环获取ArgumentOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Blazor,并且在尝试使用for循环加载复选框列表时遇到了此异常.

I'm trying out Blazor and I'm getting this exception when I'm trying to load a list of checkboxes using a for loop.

我已经在这里创建了列表:

I've created the list here:

public List<Month> Months { get; set; }

protected override void OnInitialized()
        {
            List<Month> months = new List<Month>()
            {
                new Month{MonthId = 0, MonthName = "All Months", isMonthChecked = false},
                new Month{MonthId = 1, MonthName = "Jan", isMonthChecked = false},
                new Month{MonthId = 2, MonthName = "Feb", isMonthChecked = false},
                new Month{MonthId = 3, MonthName = "Mar", isMonthChecked = false},
                new Month{MonthId = 4, MonthName = "Apr", isMonthChecked = false},
                new Month{MonthId = 5, MonthName = "May", isMonthChecked = false},
                new Month{MonthId = 6, MonthName = "Jun", isMonthChecked = false},
                new Month{MonthId = 7, MonthName = "Jul", isMonthChecked = false},
                new Month{MonthId = 8, MonthName = "Aug", isMonthChecked = false},
                new Month{MonthId = 9, MonthName = "Sep", isMonthChecked = false},
                new Month{MonthId = 10, MonthName = "Oct", isMonthChecked = false},
                new Month{MonthId = 11, MonthName = "Nov", isMonthChecked = false},
                new Month{MonthId = 12, MonthName = "Dec", isMonthChecked = false}
            };
};

            Months = months.ToList();
            base.OnInitialized();

然后,我在Blazor组件中创建了循环:

Then, I created the loop in the Blazor component:

@{ 
            for (int i = 0; i < @Months.Count(); i++)
            {
            <ul>
                <li><label for="@Months[i].MonthId" id="checkboxLabel">@Months[i].MonthName</label></li>
                <li><InputCheckbox id="@Months[i].MonthId" @bind-Value="@Months[i].isMonthChecked"></InputCheckbox></li>
            </ul>
            }
        }

我已逐步调试程序,并正在提取正确的数据,但完成后,它会给我错误.如果我将计数减少到12(应为13),则可以使用.不知道这是怎么回事.

I've stepped through the debugger and it is pulling the correct data, but when it finishes, it gives me the error. If I reduce the count to 12 (should be 13), it works. Not sure what is going on here.

推荐答案

您可以这样解决:

@for (int j = 0; j < Months.Count; j++)
{
    int i = j;  // a local copy to solve the capture problem

    <ul>
        <li><label for="@Months[i].MonthId" id="checkboxLabel">@Months[i].MonthName</label></li>
        <li><InputCheckbox id="@Months[i].MonthId" @bind-Value="@Months[i].isMonthChecked"></InputCheckbox></li>
    </ul>
}

这意味着这是.

当您查看<pagename>.razor.g.cs文件时,您会看到该复选框需要两个lambda,这是一个代码段:

When you look at the <pagename>.razor.g.cs file you can see that the checkbox requires two lambda's, here is a snippet:

 __value => Months[i].isMonthChecked = __value

,在for循环之后,当构建RenderTree时,i的值将为所有月份的Count.

and after the for-loop, when the RenderTree is being built, the value of i will be Count for all months.

在原始代码中使用Count()-1(您的临时修订)可以防止出现异常,但请注意,该表格将无法正常运行,将检查错误的月份.

Using Count()-1 in the original code (your temporary fix) will prevent the exception but note that the form will not function properly, the wrong month will be checked.

一般结论:不要将EditForm与for循环混合使用. foreach()是安全的.

General conclusion: don't mix EditForm with a for-loop. foreach() is safe.

这篇关于在Blazor组件中使用for循环获取ArgumentOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆