Lasse Edsvik
Lasse Edsvik

Reputation: 9298

Get checked values from array

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

Upvotes: 0

Views: 143

Answers (2)

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

Assuming TimeRange1 is your CheckBox[], try this:

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

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191036

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))
{
 ....
}

Upvotes: 1

Related Questions