Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net MVC checkbox headache!

I have seen lots of questions relating to this topic.

I am using asp.net MVC 1.0

Problem area

If I use

<%= Html.CheckBox("Status", true) %>

Then why It renders like

<input checked="checked" id="Status" name="Status" type="checkbox" value="true" /><input name="Status" type="hidden" value="false" />

I put this in foreach loop and I have 5 rows.

when I submit form with true,true,true,false,false then I get true,false,true,false,true,false,false,false

i.e. for false => false.

for true => true,false

If I use

<input type="checkbox" value="true" name="Status" checked="checked" />

Then I don't get unchecked one's.

so how do I overcome form this problem?

Please don't post answer with using loop in formcollection object and checking each key!

like image 989
Vikas Avatar asked Jun 08 '09 06:06

Vikas


4 Answers

I know this isn't the elegant one but this is what I did:

collection["response"].Replace("true,false", "true").Split(',').ToList();

like image 61
RailRhoad Avatar answered Nov 11 '22 01:11

RailRhoad


In your example, when you submit form with true,true,true,false,false and you get

true,false,true,false,true,false,false,false
it is interesting to note that you are not actually getting eight values back, but five arrays that merely looks like this is the case because all of the values are joined.

I know you asked to not get a loop for your answer, but I can use one to demonstrate what is really happening here:
foreach (string key in postedForm.AllKeys) {
    // "value" will contain a joined/comma-separated list of ALL values,
    // including something like "true,false" for a checked checkbox.
    string value = postedForm[key].GetValue;
    // "values" will be an array, where you can access its first element,
    // e.g., values[0], to get the actual intended value.
    string[] values = postedForm.GetValues(key);
}

So, for your checked boxes, you'll get a values array with two elements, and for unchecked boxes, you'll get just a single-element array.

Thus, to answer your question how do you overcome this problem, the answer lies in using GetValues rather than GetValue, and thinking of your posted fields as arrays rather than strings.

Best of luck!

like image 39
Funka Avatar answered Nov 11 '22 02:11

Funka


Personally I think having to check for "true,false" everywhere on the server is a pain. I wrote a jquery fix that will remove the extra hidden field created by the Html.Checkbox helper when a box is checked, then add it back if the box is unchecked. Server values will always be "true" or "false". Checkbox lists are kind of subjective in how you want them to act, which I discuss, but I'm removing "false" from the value set, which means the form value will be excluded if all boxes in the list are unchecked.

http://www.mindstorminteractive.com/blog/?p=386

I've had pretty good success using this technique. Please let me know if you try it out and have issues.

like image 1
Mindstorm Interactive Avatar answered Nov 11 '22 01:11

Mindstorm Interactive


You'll have to do your own model binding for the CheckBox values.

Get the list of values from the FormCollection or Request.Form for that CheckBox id and replace true,false with true:

string chkBoxString = Request.Form["checkboxID"].Replace("true,false", "true")

Now you have a list of whether a CheckBox was selected or not.... do the rest yourself :)

like image 1
ShaneGray Avatar answered Nov 11 '22 01:11

ShaneGray