I am using the following syntax to create a checkbox:
<%: Html.CheckBox("Monday", new { id = "Monday" })%>
<label for="Monday">
Monday</label>
in asp.net MVC2 view and when I try to get it in controller action like this:
string Monday = Request["Monday"];
Request[“Monday”] has value “true,false”. why so? How can I get current value (checked/unchecked of checkbox)
[EDITED]
<fieldset>
<legend>Week days</legend>
<%: Html.CheckBox("Monday", new { id = "Monday" })%>
<label for="Monday">
Monday</label>
<%: Html.CheckBox("Tuesday", false, new { id = "Tuesday" })%>
<label for="Tuesday">
Tuesday</label>
<%: Html.CheckBox("Wednesday", false, new { id = "Wednesday" })%>
<label for="Wednesday">
Wednesday</label>
<%: Html.CheckBox("Thrusday", false, new { id = "Thrusday" })%>
<label for="Thrusday">
Thrusday</label>
<%: Html.CheckBox("Friday", false, new { id = "Friday" })%>
<label for="Friday">
Friday</label>
<%: Html.CheckBox("Saturday", false, new { id = "Saturday" })%>
<label for="Saturday">
Saturday</label>
<%: Html.CheckBox("Sunday", false, new { id = "Sunday" })%>
<label for="Sunday">
Sunday</label>
</fieldset>
Please suggest
In your situation, there is no much use in using the HtmlHelper to create your checkbox. It would simply by easier to make the code yourself:
Then you could access the value when you post like so:
Alternatively, you could make a view model and make a checkbox like so:
Additionally, when you use a helper like this, it actually generates the ID for you, so you dont need to assign the id of the value (aka, no
{ id = "monday" }is required). In your situation, it sounds like you have assigned the same ‘name` to one or more elements on your form, and thus the values will be concatenated together with a comma. I’d check to see if your page contains another element with the same name attribute.