0

I'm using mvc and I'm trying to loop through a array of checkboxes, but how do I exclude the ones that are "false" in that list?

for(int i = 0; i < TimeRange1.Length; i++)
        {
          if(TimeRange1[i] == "false" ....??)
        // dostuff
        }

or is there some better way of doing it?

/M

2
  • if TimeRange1 is a checkbox array (see Tim S. Van Haren) then you should use better names like cbTimeRanges Commented Nov 12, 2009 at 14:49
  • Please show the relevant portion of your view. Specifically, the Html.CheckBox() helper is meant to render only single checkboxes and must not be used to render a collection of checkboxes with the same name. Commented Nov 12, 2009 at 19:47

2 Answers 2

1

Since you are doing it with MVC - you could make TimeRange1 a bool[].

Then, you could always do this with linq

var newItems = TimeRange1.Select(i => i == false);

foreach(var item in newItems)
{
 ....
}

or you could simplify it

foreach(var item in TimeRange1.Select(i => i == false))
{
 ....
}
Sign up to request clarification or add additional context in comments.

3 Comments

how do I get: foreach(var item in TimeRange1.Select(i => i != "false")) to work? I cant seem to get hold of the value
You can't not a string value. Remove the quotes.
...& change the type of TimeRange1.
1

Assuming TimeRange1 is your CheckBox[], try this:

for (int i = 0; i < TimeRange1.Length; i++)
{
    if (TimeRange1[i] == "on")
    {
       // dostuff
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.