自在修行 2021-12-02 22:21 采纳率: 0%
浏览 24

使用 antDesign blazor 时,@for语句嵌套<Radio 循环异常问题索引溢出问题

在使用antDesign blazor 时

@code{
       string[]  选项list ="张三,李四".Split(',');
}
    <RadioGroup @bind-Value="@radioValue">
    @for(int i=0;i<选项list.Count();i++)
    {
        <Radio Value="@i">@选项list[i]</Radio>  //这个位置总报  索引超出范围
    }

    </RadioGroup>
   

img


如上代码,在运行时总报 ”System.IndexOutOfRangeException:“Index was outside the bounds of the array.”"
我单步执行了一下,竟然出现循环完成后又再次执行了 Radio 语句
无法理解

我想要达到的结果

我的代码错在哪里?

  • 写回答

2条回答 默认 最新

  • 自在修行 2021-12-03 10:44
    关注

    找到原因了
    https://docs.microsoft.com/zh-cn/aspnet/core/blazor/components/?view=aspnetcore-6.0&WT.mc_id=DT-MVP-5003987#child-content

    img


    改成下面就可以了

     <RadioGroup @bind-Value="@radioValue">
        @for(int i=0;i<选项list.Length;i++)
        {
           var index=i; 
            <Radio Value="@index">@选项list[index]</Radio>  //这个位置总报  索引超出范围
    
        }
        </RadioGroup>
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 12月2日