Reputation: 2397
I have a form containing the following:
<form method="post">
<input type="checkbox" name="a1" >
In my Razor/C# code I want to assign to an integer var a1
the value of 1
is the element is checked and the value of 0
if unchecked.
I've been trying things like
If (document.getElementById("a1").checked)
If (document.getElementById("a1").checked==true)
If (document.getElementById("a1").checked.AsBool()==true)
If (Request.Form["a1"].checked==true)
And other similar permutations of the above, and nothing works. Either I have error messages, or it ignores me.
How do you this?
Clarification: the first 3 lines above are in a inside <script> </script>
tags inside the Razor code.
Ultimately, I need to UPDATE
a record in a table with this.
Upvotes: 1
Views: 5548
Reputation: 2397
the answer was simple:
var a1=(Request.Form["a1"]=="on") ? 1 : 0;
The value I was looking for is a string, without any .checked
or anything like that.
And then I just do a conditional assignment.
Upvotes: 2
Reputation: 19656
Assuming you mean in JavaScript - You need to set the 'id' attribute if you wish to use getElementById
<input type="checkbox" name="a1" id="a1">
Upvotes: 2