C
C#2y ago
PatrickG

❔ Binding a list<tuple<string,bool> to a form with switches in asp.net

does anyone know how I can send a list of tuple string bool to a mvc page and display a checkbox in a form that has the string as the name and the bool as the checked or not i am able to display the switches but I have no idea how to make the submit work ive asked chat gpt and tried everything it keeps returning null. in asp.net 5 mvc btw
27 Replies
Angius
Angius2y ago
var data = new List<(bool checked, string name)>() {
(true, "cat"),
(false, "dog"),
(true, "parrot")
};
return View(data);
var data = new List<(bool checked, string name)>() {
(true, "cat"),
(false, "dog"),
(true, "parrot")
};
return View(data);
@foreach (var tup in Model)
{
<label for="[email protected]">@tup.name</label>
<input
name="@tup.name"
type="checkbox"
checked="@tup.checked">
}
@foreach (var tup in Model)
{
<label for="[email protected]">@tup.name</label>
<input
name="@tup.name"
type="checkbox"
checked="@tup.checked">
}
Something like this
PatrickG
PatrickGOP2y ago
i cant name the bool and string List<(bool,string) works but not with names
Angius
Angius2y ago
Yep, you're right
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
sealed record CheckboxItem(bool Checked, string Name);

var data = new List<CheckboxItem>() {
new (true, "cat"),
new (false, "dog"),
new (true, "parrot")
};
data
sealed record CheckboxItem(bool Checked, string Name);

var data = new List<CheckboxItem>() {
new (true, "cat"),
new (false, "dog"),
new (true, "parrot")
};
data
Result: List<CheckboxItem>
[
{
"checked": true,
"name": "cat"
},
{
"checked": false,
"name": "dog"
},
{
"checked": true,
"name": "parrot"
}
]
[
{
"checked": true,
"name": "cat"
},
{
"checked": false,
"name": "dog"
},
{
"checked": true,
"name": "parrot"
}
]
Compile: 556.238ms | Execution: 88.954ms | React with ❌ to remove this embed.
Angius
Angius2y ago
There, much better Records FTW
PatrickG
PatrickGOP2y ago
yea well i get same problem ive been getting all day when i submit i never receive anything
PatrickG
PatrickGOP2y ago
Angius
Angius2y ago
Show code
PatrickG
PatrickGOP2y ago
PatrickG
PatrickGOP2y ago
Angius
Angius2y ago
Where's the name on the inputs?
PatrickG
PatrickGOP2y ago
what do you mean its in the piblic record CheckboxItem
Angius
Angius2y ago
Inputs need names
Angius
Angius2y ago
: The Input (Form Input) element - HTML: HyperText Markup Language ...
The HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide variety of types of input data and control widgets are available, depending on the device and user agent. The element is one of the most powerful and complex in all of HTML due to the sheer number of combinations of input t...
PatrickG
PatrickGOP2y ago
ok let me try that again
PatrickG
PatrickGOP2y ago
PatrickG
PatrickGOP2y ago
minus the last D still get 0 data in the backend
Angius
Angius2y ago
Open devtools network panel, see what gets sent
PatrickG
PatrickGOP2y ago
ill try with a for loop and use i index
Angius
Angius2y ago
no difference between for and foreach
PatrickG
PatrickGOP2y ago
PatrickG
PatrickGOP2y ago
its not sending a list it just think its an object with cat attribute and parrot attribute
PatrickG
PatrickGOP2y ago
well i tried this cuz ive seen stack overflow post that seemed to indicate it would work but still doesnt
Angius
Angius2y ago
As I said, makes no difference whether you use a for or a foreach So, I did some experimenting...
<form action="#" method="post">
<input type="checkbox" name="a" value="cat">
<input type="checkbox" name="a" value="dog">
<button>Post</button>
</form>
<form action="#" method="post">
<input type="checkbox" name="a" value="cat">
<input type="checkbox" name="a" value="dog">
<button>Post</button>
</form>
submitting this will get you either a single value, a: cat for example, or an array should multiple values be selected, like a: [ cat, dog ] You should try giving the checkboxes the same name and different values, and binding to List<string> param
PatrickG
PatrickGOP2y ago
ok yea i got it i can rebuild the records if needed in backend thanks
Angius
Angius2y ago
No problem, glad it worked
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server