Solved

MVC checkboxes

Posted on 2013-06-23
4
434 Views
Last Modified: 2013-06-23
Hi,

I have 5 checkboxes for different Job types.  

the user can select multiple checkboxes.

my linq filter is wrong.

how can I say if the user select one or more of the checkboxes to filter it.

here's what I have

    if (Contract)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 2);
                }

                if (Intership)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 3);
                }

                if (NonProfit)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 5);
                }

                if (PartTime)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 4);
                }

                if (Telecommute)
                {
                    ResultSet = ResultSet.Where(c => c.a.JobTypesID.Value == 1);
                }

right now if I select more than one selection it will not display anything.

Thanks,
0
Comment
Question by:lulu50
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 39269287
You filter first on one id, then you filter the filtered resultset on a second id... That's like doing a logical AND on two exclusive states...  This is why you get no results.  You want something along these lines:
                List<int> filterList = new List<int>();
                if (Contract)
                {
                    filterList.Add(2);
                }

                if (Intership)
                {
                    filterList.Add(3);
                }

                if (NonProfit)
                {
                    filterList.Add(5);
                }

                if (PartTime)
                {
                    filterList.Add(4);
                }

                if (Telecommute)
                {
                    filterList.Add(1);
                }
                ResultSet = ResultSet.Where(c => c.a.JobTylesID.HasValue && filterList.Contains(c.a.JobTypesID.Value));

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 39269341
Hi lulu50;

I think something like this will give you the solution you are looking for. The main reason why you solution was not giving the wanted results is that doing multiple where clauses like you were doing is a AND operation and not OR.

List<int> jobType = new List<int>();

if (Contract) jobType.Add(2);
if (Intership) jobType.Add(3);
if (NonProfit) jobType.Add(5);
if (PartTime) jobType.Add(4);
if (Telecommute) jobType.Add(1);

ResultSet = "The first part of the query HERE".Where(c => jobType.Contains(c.a.JobTypesID.Value));

Open in new window

0
 

Author Closing Comment

by:lulu50
ID: 39269460
Thank you
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39269631
Not a problem lulu50, glad to help. ;=)
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question