This question could already be answered, but I wanted to explain your problem so that others could understand what was happening.
You do not know that you have already set the value false to your input, since you are using attributes incorrectly.
Let's look at your view
@model GroupIndexViewModel <form asp-action="Index" asp-controller="Group" method="get"> <ul> @for (var i = 0; i < Model.Filters.Length; i++) { <li> <input type="checkbox" id="@Model.Filters[i].Name" asp-for="@Model.Filters[i].Selected" value="@Model.Filters[i].Selected" checked="@Model.Filters[i].Selected" /> <label for="@Model.Filters[i].Name">@Model.Filters[i].Name</label> </li> } </ul> <button type="submit" name="action">Filtrer</button> </form>
So first. You create input elements from the Filter array. Now let's take a closer look at your input element.
<input type="checkbox" id="@Model.Filters[i].Name" asp-for="@Model.Filters[i].Selected" value="@Model.Filters[i].Selected" checked="@Model.Filters[i].Selected" />
Now let me explain this.
- You specify the type using the
type attribute. - You specify the identifier using the
id attribute. - You bind input to the model using the
asp-for tag helper. - You specify the value to enter using the
value attribute. - Finally, you set the input as marked using the
checked attribute.
If you look at the Tag Helper Documentation , you will find the connection between the .Net Type and Input Type , realizing that:
The flags contain a logical value corresponding to the model and formatted by the tag helper as type="checkbox" .
Since you use the type="checkbox" attribute, the Tag Helper value can only be true or false . So, if we go back and look at the input element, you are already specifying a value for the input. Although the tag assistant can assign a value to the input, it cannot override what is already specified. Therefore, your input will always have the value you specify, in this case boolean is always false by default.
Now you might think that your input element is false , and for example, adding checked="checked" will not change the value to true , since the value attributes override the checked attribute. Causes poor implementation of both attributes.
Therefore, you should use only one attribute. (Either value or checked ). You can use them for convenience. But in this case, you should use the default attribute checked . Since you are implementing a Tag Helper and an input value, they must be of type boolean . And the value of the checked attribute returns a boolean and, for example, this is the value used by the tag assistant.
Thus, the implementation provided by @dotnetstep should work, as it only declares an auxiliary tag in the input element. Thus, Tag Helper processes the corresponding input attributes.
@model GroupIndexViewModel <form asp-action="Index" asp-controller="Group" method="get"> <ul> @for (var i = 0; i < Model.Filters.Count; i++) { <li> <input type="checkbox" asp-for="@Model.Filters[i].Selected" /> <label asp-for="@Model.Filters[i].Selected">@Model.Filters[i].Name</label> <input type="hidden" asp-for="@Model.Filters[i].Id" /> <input type="hidden" asp-for="@Model.Filters[i].Name" /> </li> } </ul> <button type="submit" name="action">Filtrer</button> </form>