I am looking to implement a checkboxlist in ASP.NET Core, but am facing some difficulties.
My ViewModel:
public class GroupIndexViewModel
{
public
You are not aware that you are already specifying the false
value to your input, since you're implementing a bad use of attributes.
Let's take a look at your view
@model GroupIndexViewModel
So, first. You are creating input elements from an array of Filter
. Now let's take a closer look at your input element.
Now, let me explain this.
type
attribute.id
attribute.asp-for
tag helper.value
attribute.checked
attribute.If you take a look at the Tag Helpers Documentation, you'll find the relationship between the .Net Type and the Input Type, understanding that:
Checkboxes holds a boolean value, corresponding to the model, and formatted by the tag helper as
type="checkbox"
.
Since you're using 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're already specifying a value to the input. Even though the tag-helper can assign a value to the input, it can't override the one already specified. Therefore, your input, will always have the value you've specified, in this case, a boolean
is always false by default.
Now you might think that your input element, has a false
value, and for instance, adding the checked="checked"
will not change the value to true
, since the value
attributes, overrides the checked
attribute. Causing a bad implementation of both attributes.
Therefore, you must use only one of the attributes. (Either the value
or the checked
). You can use them, for convenience. But in this case, you must use the default checked
attribute. Since you're implementing a Tag Helper, and the input value, have to be of type boolean
. And the checked
attribute value, returns a boolean and for instance, is the one used by the Tag Helper.
So the implementation provided by @dotnetstep should work, since it only declares the tag helper in the input element. So the Tag Helper, handles itself the corresponding attributes of the input.
@model GroupIndexViewModel