I'm filtering out products by gender using checkboxes.
Male
Female
I start by checking IF there's a queryString containing the gender value and if there's not, I'll just use my string Gender which by default should have all the checkbox values (male & female).
But I can't use my string gender in my range. How can I choose both male & female from a Checkboxlist?
string gender = "male, female";
if (queryString.Get("gender") != null)
{
gender = queryString.Get("gender");
}
selectedItems.AddRange(productTypes.Where(x => x.Parent.GetPropertyValue<string>("genderList") == gender));
itemCount = selectedItems.Count();
foreach (var item in selectedItems.Skip((page - 1) * pageSize).Take(pageSize))
{
/* If pricerange is selected */
@buildItemProduct(item);
}
Some bikes have NO gender/are null because it's unisex bikes. Right now, you'll see only unisex bikes if you choose Specialized which has ALL bikes of every subcategory.
While being on Specialized I'd like to show both null, male and female at the same time.
In other words
Is it possible to have a string gender = ?? that has ALL the checkbox values? So if I call gender it'll be both male & female?
The first part checks there is a value, i.e. it's not null. If it is null then it will pass, if it is not null, then it gets value splits it into an array based on a , (as I think the check list will store as a comma separated list) and then checks if any of those entries are in the gender variable.
If that doesn't work, it will just be a case of tweaking the logic there as that should do what you need.
Nik, I have a quick question though. (nothing is ever quick)
The solution you can up with work as a charm and for that I'm really happy :o)
But if I choose a price, it seems to mess it up somehow. I thought I could just take you're solution and throw it inside my range for when a price is selected.
If you go here -> Select Mand/Male -> Select the lowest pricerange
Apparently, It takes EVERY unisex/null bike, eventhough it's not in the pricerange.
I'm not sure how to use your solution AND check for prices at the same time:
if (priceArray.Count() == 2 && int.TryParse(priceArray[0], out minPrice) && int.TryParse(priceArray[1], out maxPrice))
{
selectedItems.AddRange(productTypes .Where(x =>
!x.Parent.HasValue("genderList")
|| x.Parent.GetPropertyValue<string>("genderList").Split(',').Any(g => gender.Contains(g))
&& x.HasValue("price")
&& x.GetPropertyValue<int>("price") > minPrice
&& x.GetPropertyValue<int>("price") < maxPrice));
itemCount = selectedItems.Count();
foreach (var item in selectedItems.Skip((page - 1) * pageSize).Take(pageSize))
{
/* If pricerange is selected */
@buildItemProduct(item);
}
}
(Notice I added >= this means that the price can equal the minimum or the maximum values of your range else you would never see something that cost 5000kr or 9999kr if you chose the 5000 - 9999kr range
The grouping in this case case been done using brackets, an alternative (although less efficient) is to break it out into different selects:
Nik & Ian, you guys are epic, I'm sitting here with my hands in the air unable to take them down. I managed to group my conditions based on your code and it WORKS
THANK you both for taking the time to help me solve this. I have learned a great deal thanks to you two.
Store Checkboxlist values in a string
I'm filtering out products by gender using checkboxes.
I start by checking IF there's a queryString containing the gender value and if there's not, I'll just use my
string Gender
which by default should have all the checkbox values (male & female).But I can't use my string gender in my range. How can I choose both male & female from a Checkboxlist?
Hi Mike,
There are two options that I initially think of:
You could change your where function to do the following:
The other is (and this not knowing how the Gender query string will look if there are multiple:
You could also add a second param to the g.Equals method to make it case insensitive.
Hope that helps.
Nik
Hey Nik,
Thank you for your reply!
I tried your first suggestion but it tells me the
Value cannot be null.
A live example can be seen here
A bike can be:
Some bikes have NO gender/are null because it's unisex bikes. Right now, you'll see only unisex bikes if you choose Specialized which has ALL bikes of every subcategory.
While being on Specialized I'd like to show both null, male and female at the same time.
In other words
Is it possible to have a
string gender = ??
that has ALL the checkbox values? So if I callgender
it'll be both male & female?Ahh, if things can be null (i.e. uni sex) try changing the statement to this:
The first part checks there is a value, i.e. it's not null. If it is null then it will pass, if it is not null, then it gets value splits it into an array based on a , (as I think the check list will store as a comma separated list) and then checks if any of those entries are in the gender variable.
If that doesn't work, it will just be a case of tweaking the logic there as that should do what you need.
Nik
YES! It works, you are the man Nik!
Thank you for helping me through this. Not only did I learn something new - my most annoying problem has been solved aswell.
Thank you for the solution!
Nik, I have a quick question though. (nothing is ever quick)
The solution you can up with work as a charm and for that I'm really happy :o)
But if I choose a price, it seems to mess it up somehow. I thought I could just take you're solution and throw it inside my range for when a price is selected.
If you go here -> Select Mand/Male -> Select the lowest pricerange
Apparently, It takes EVERY unisex/null bike, eventhough it's not in the pricerange.
I'm not sure how to use your solution AND check for prices at the same time:
Hi Mike,
Okay, so what you need to do is group your conditions by using brackets.
You have 2 sets of conditions.
1) Does it meet the gender requirements 2) Does it fall into the price range.
So, a quick mock might looks something like this:
(Notice I added >= this means that the price can equal the minimum or the maximum values of your range else you would never see something that cost 5000kr or 9999kr if you chose the 5000 - 9999kr range
The grouping in this case case been done using brackets, an alternative (although less efficient) is to break it out into different selects:
Good luck :-)
Would it solve your problem to group your conditions?
No problem Mike, pleased it helped :-)
Nik & Ian, you guys are epic, I'm sitting here with my hands in the air unable to take them down. I managed to group my conditions based on your code and it WORKS
THANK you both for taking the time to help me solve this. I have learned a great deal thanks to you two.
is working on a reply...