Why is the binding of my Blazor InputCheckbox component not working as expected?

I’m using an EditForm bound to a model to filter some data in my Blazor application. As part of this, I have a InputCheckbox for each item in a list, which is bound to a bool attribute of that individual item. When I populate the list of objects of this class, I’m setting the initial value of the Selected to be true, so that every item is checked by default.

When the EditForm is submitted, however, the value of the attribute for the given item remains true, even if I have unchecked the box.

This is my EditForm:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><EditForm Model="FilterCriteria" OnSubmit="PerformFiltering" FormName="Filtering">
<label>Test Properties</label>
@foreach (var property in Properties)
{
<label>
<InputCheckbox name="property" @bind-Value="FilterCriteria.SelectedProperties.Where(p => p.PropertyId == property.Id).FirstOrDefault().Selected" />
@property.Name
</label>
}
<button type="submit">Search</button>
</EditForm>
</code>
<code><EditForm Model="FilterCriteria" OnSubmit="PerformFiltering" FormName="Filtering"> <label>Test Properties</label> @foreach (var property in Properties) { <label> <InputCheckbox name="property" @bind-Value="FilterCriteria.SelectedProperties.Where(p => p.PropertyId == property.Id).FirstOrDefault().Selected" /> @property.Name </label> } <button type="submit">Search</button> </EditForm> </code>
<EditForm Model="FilterCriteria" OnSubmit="PerformFiltering" FormName="Filtering">
    <label>Test Properties</label>
    @foreach (var property in Properties)
    {
        <label>
            <InputCheckbox name="property" @bind-Value="FilterCriteria.SelectedProperties.Where(p => p.PropertyId == property.Id).FirstOrDefault().Selected" />
            @property.Name
        </label>
    }
    <button type="submit">Search</button>
</EditForm>

This is the partial class for my razor component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public partial class Home
{
private List<Property> Properties { get; set; }
[SupplyParameterFromForm]
private FilteringCriteriaModel? FilterCriteria { get; set; }
protected override async Task OnInitializedAsync()
{
Properties = new List<Property>();
for (var i = 0; i < 10; i++)
{
Properties.Add(new Property
{
Id = Guid.NewGuid(),
Name = i.ToString()
});
}
FilterCriteria = new FilteringCriteriaModel();
foreach (var property in Properties)
{
FilterCriteria.SelectedProperties.Add(new PropertySelection
{
PropertyId = property.Id,
Selected = true
});
}
}
private void PerformFiltering()
{
foreach (var property in FilterCriteria.SelectedProperties)
{
Console.WriteLine($"{property.PropertyId}: {property.Selected}");
}
return;
}
}
</code>
<code>public partial class Home { private List<Property> Properties { get; set; } [SupplyParameterFromForm] private FilteringCriteriaModel? FilterCriteria { get; set; } protected override async Task OnInitializedAsync() { Properties = new List<Property>(); for (var i = 0; i < 10; i++) { Properties.Add(new Property { Id = Guid.NewGuid(), Name = i.ToString() }); } FilterCriteria = new FilteringCriteriaModel(); foreach (var property in Properties) { FilterCriteria.SelectedProperties.Add(new PropertySelection { PropertyId = property.Id, Selected = true }); } } private void PerformFiltering() { foreach (var property in FilterCriteria.SelectedProperties) { Console.WriteLine($"{property.PropertyId}: {property.Selected}"); } return; } } </code>
public partial class Home
{
    private List<Property> Properties { get; set; }

    [SupplyParameterFromForm]
    private FilteringCriteriaModel? FilterCriteria { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Properties = new List<Property>();

        for (var i = 0; i < 10; i++)
        {
            Properties.Add(new Property
            {
                Id = Guid.NewGuid(),
                Name = i.ToString()
            });
        }

        FilterCriteria = new FilteringCriteriaModel();


        foreach (var property in Properties)
        {
            FilterCriteria.SelectedProperties.Add(new PropertySelection
            {
                PropertyId = property.Id,
                Selected = true
            });
        }
    }

    private void PerformFiltering()
    {
        foreach (var property in FilterCriteria.SelectedProperties)
        {
            Console.WriteLine($"{property.PropertyId}: {property.Selected}");
        }
        return;
    }
}

This is the Property class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Property
{
public Guid Id { get; set; }
public string Name { get; set; }
}
</code>
<code>public class Property { public Guid Id { get; set; } public string Name { get; set; } } </code>
public class Property
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

This is the PropertySelection class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class PropertySelection
{
public Guid PropertyId { get; set; }
public bool Selected { get; set; }
}
</code>
<code>public class PropertySelection { public Guid PropertyId { get; set; } public bool Selected { get; set; } } </code>
public class PropertySelection
{
    public Guid PropertyId { get; set; }
    public bool Selected { get; set; }
}

This is the FilteringCriteriaModel class:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class FilteringCriteriaModel
{
public List<PropertySelection> SelectedProperties = new List<PropertySelection>();
}
</code>
<code>public class FilteringCriteriaModel { public List<PropertySelection> SelectedProperties = new List<PropertySelection>(); } </code>
public class FilteringCriteriaModel
{
    public List<PropertySelection> SelectedProperties = new List<PropertySelection>();
}

I’m using ASP.NET Core Blazor, on .NET 8.

Any and all help is much appreciated 🙂

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật