-1

I've got 5 checkboxes in my View, all set to unchecked upon intial page load. Each of the checkboxes has a textbox associated with it.

One, none or multiple can be checked at any one time, which then gets sent to my Controller.

At the controller level, I'd love to know if there is a more efficient method of finding out which of the many checkboxes have been checked.

At the moment, I'm doing:

if ((checkBox1 == true ) && (checkBox2 == true) && (checkBox3 = true) && (checkBox 4 = true) && (checkBox5 == true))
{
do something with all the textBoxes associated with the checkBoxes that are checked
}
elseif ((checkBox1 == true ) && (checkBox2 == true) && (checkBox3 = true) && (checkBox 4 = true))
{
do something with all the textBoxes associated with the checkBoxes that are checked
}
......and so on

Is there a more efficient method of doing this, or am I going to spend the next few hours copying and pasting :)

Thanks

ma11achy
  • 133
  • 2
  • 14
  • Possible duplicate of [How to get all CheckBoxes using C#?](http://stackoverflow.com/questions/8516102/how-to-get-all-checkboxes-using-c) – emerson.marini Jan 20 '15 at 15:09
  • also http://stackoverflow.com/questions/11130381/c-sharp-cleaner-way-to-check-for-checkbox-states?rq=1 – dkiefer Jan 20 '15 at 15:10
  • Also [Getting all selected checkboxes from a FormCollection](http://stackoverflow.com/questions/3831606/getting-all-selected-checkboxes-from-a-formcollection) – emerson.marini Jan 20 '15 at 15:11
  • All those questions are just checkBoxes - my question is related to mixing checkboxes and textboxes. Please read before marking as duplicate – ma11achy Jan 20 '15 at 15:20
  • So that prevents you from adapting these other already answered questions to your current problem? As in my link, you can't use a for loop to check if your boxes are checked and then perform your textbox logic? – dkiefer Jan 20 '15 at 15:41
  • 1
    The problem here is that you have not included the the most important part.. that is WHAT are you doing with the textboxes? Why is that the most important part? Because that influences the logic you can use in the checkbox validation. There are any number of different ways you can write logic for this, but it all depends on what exactly you're doing with the data within those braces. – Erik Funkenbusch Jan 20 '15 at 15:58

1 Answers1

0

Your question is a bit vague so I don't know exactly what your are attempting but I have an MVC view that renders multiple textboxes and checkboxes. In my model I have a list of items where each item is one 'row' on the form (i.e a label, a text box and a check box). On post back I simply iterate my list to process the check boxes.

Sample Page

This page allows a place to be included/excluded or it's name overridden... before I go pasting examples is this the sort of thing you are doing? (specifically are any of your checkboxes aware of the other checkboxes?)

If you want to check the state of each check box in relation to the others try this:

    bool c1 = true;  // checkbox1.Checked
    bool c2 = true;  // checkbox2.Checked
    bool c3 = false; // etc
    bool c4 = true;

    BitArray arr = new BitArray(new bool[4] { c1, c2, c3, c4 });
    byte[] bits = new byte[4];
    arr.CopyTo(bits, 0);
    int x = BitConverter.ToInt32(bits, 0);

This will give 'x' a value for each combination (simple binary) so 1=c1, 3=c1 & c2, 11=c1 & c2 & c4. This is much more readable and if you add the sixth and seventh checkbox in the future you wont have hundreds of lines of code to add.

Dave Becker
  • 1,433
  • 1
  • 12
  • 24