Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Blazor中的数据模型列表生成复选框列表时出现问题_C#_Blazor - Fatal编程技术网

C# 从Blazor中的数据模型列表生成复选框列表时出现问题

C# 从Blazor中的数据模型列表生成复选框列表时出现问题,c#,blazor,C#,Blazor,有谁知道如何初始化只有myPicketItemsStoredInDabase项标记为选中且所有其他项标记为未选中的复选框列表 当我尝试下面的代码示例时,所有复选框都标记为选中。我尝试过不同的解决方案,但没有一个方案在upp结束时会显示一个复选框列表,并将正确的项目标记为选中 public class CheckboxItem { public Guid Id { get; set; } public string Name { get; set; } public boo

有谁知道如何初始化只有myPicketItemsStoredInDabase项标记为选中且所有其他项标记为未选中的复选框列表

当我尝试下面的代码示例时,所有复选框都标记为选中。我尝试过不同的解决方案,但没有一个方案在upp结束时会显示一个复选框列表,并将正确的项目标记为选中

public class CheckboxItem
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
}

public class ItemFromDatabase
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}
佩奇剃须刀

@foreach (var item in itemsToEdit)
{
    <CheckBoxItemComponent Items="@itemsToEdit" Value="@item.Name" Item="@item" />
}

@code {
    private IEnumerable<ItemFromDatabase> myPickedItemsStoredInDatabase;
    private IEnumerable<ItemFromDatabase> allItems;
    private List<CheckboxItem> itemsToEdit;

    protected override async Task OnInitializedAsync()
    {
        myPickedItemsStoredInDatabase = await getFromJsonAsync<ItemFromDatabase>.....
        allItems= await getFromJsonAsync<ItemFromDatabase>.....
        InitCheckboxItems();
    }

    private void InitCheckboxItems()
    {
        itemsToEdit = new List<CheckboxItem>();

        for (int i = 0; i < allItems.Length; i++)
        {
            CheckboxItem item = new CheckboxItem()
            {
                Id = allItems[i].Id,
                Name = allItems[i].Name,
                Checked = false
            };

            if (myPickedItemsStoredInDatabase.Any(p => p.Id == item.Id))
            {
                item.Checked = true;
            }

            itemsToEdit.Add(item);
        }
    }
}
@foreach(itemsToEdit中的变量项)
{
}
@代码{
私有IEnumerable MyPickEditemsStoredDatabase;
私人可数Allitem;
私人列表项目编辑;
受保护的重写异步任务OnInitializedAsync()
{
myPickedItemsStoredInDatabase=等待getFromJsonAsync。。。。。
allItems=等待getFromJsonAsync。。。。。
InitCheckboxItems();
}
私有void InitCheckboxItems()
{
itemsToEdit=新列表();
for(int i=0;ip.Id==item.Id))
{
item.Checked=true;
}
itemsToEdit.Add(项目);
}
}
}
CheckBoxItemComponent.razor

@Item.Name
@代码 { [参数] 公共字符串值{get;set;} [参数] 公共CheckboxItem项{get;set;} [参数] 公共列表项{get;set;} 私人住宅被检查 { get=>Items.Any(el=>el.Name==Value); 设置 { 如果(值) { 如果(!Items.Any(el=>el.Name==Value)) 项目。添加(项目); } 其他的 项目。移除(项目); } } }`
我不明白在我的检查中会发生什么。
要设置为选中,可以修改CheckBoxItemComponent.razor,如下所示:

<input type="checkbox" @bind=isChecked /> @Item.Name
<br />
@code
{

    [Parameter]
    public CheckboxItem Item { get; set; }

    public bool isChecked
    {
        get => this.Item.Checked;
        set => this.Item.Checked = value;
    }
}
@Item.Name

@代码 { [参数] 公共CheckboxItem项{get;set;} 公共场所被检查 { get=>this.Item.Checked; set=>this.Item.Checked=值; } }
并在Page.razor中使用它:

@foreach (var item in itemsToEdit)
{
    <CheckBoxItemComponent Item="@item" />
}
@foreach(itemsToEdit中的变量项)
{
}

此答案已被撤回,因为它没有正确回答问题。

@HenkHolterman。正如我说的“五月”。从Blazor早期开始编写这些代码,我很快就把使用局部循环变量作为理所当然的事情,而没有调查并意识到FOR是罪魁祸首。你每天都在学习新的东西。谢谢你平贝克,你帮助了我的一天!你的解决方案解决了这个问题。
@foreach (var item in itemsToEdit)
{
    <CheckBoxItemComponent Item="@item" />
}