I am able to add the asp tag helpers asp-for to text input elements like this:
<label asp-for="Year" class="control-label"></label>
<input asp-for="Year" type="text" class="form-control" id="Year" />
And I get the data in the controller.
But, how do you do the same for checkboxes that are checked like the ones below so that I can access them in the controller?
I am trying to bind all the checked checkboxes to this model property: public List<string> PTList { get; set; }
<div class="col-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="U100" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">
U100
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="U110" id="flexCheckChecked" checked>
<label class="form-check-label" for="flexCheckChecked">
U110
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="U130" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">
U130
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="U150" id="flexCheckChecked" checked>
<label class="form-check-label" for="flexCheckChecked">
U150
</label>
</div>
</div>
2
Answers
You can use
name
atttribute to pass the checkbox value ,likeresult:
To access values in the controller you can do the following:
1. Make sure you have your html form properly set
2. Use the name attribute
2. In the controller: