CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2017
    Posts
    9

    MVC List Not Working as it should

    Hi Everyone,

    So the issue that I am having is getting the correct db items to work in the search in the web page, for some reason it does not search all of the values from Vehicle_Make that exist in Vehicle_Details - Make_ID is the primary key of Vehicle_Make and it is a FK in Vehicle_Details.

    What happens is, it shows all the items on the page in a list, but the search does not work when I search for any of those items.

    PHP Code:
     [HttpPost]
            public 
    ActionResult Search(string Location)
            {

                   
    GoogleMapEntities GE = new GoogleMapEntities();

                
    ////SELECT Make_Name DATA FROM DB1
                
    var result GE.Vehicle_Make.Where(=> x.Make_Name.StartsWith(Location)).ToList();
                var 
    GetVeh = (from vd in db.Vehicle_Details
                              join vm in db
    .Vehicle_Make
                              on vd
    .Make_ID equals vm.Make_ID
                              join u in db
    .User
                              on vd
    .User_ID equals u.User_ID
                              select vd
    ).ToList();

                
    //SELECT ALL ELEMENTS FROM Veh Make TABLE THAT EXISTS ON Veh Details TABLE BASED ON EXISTING ID's            
                
    var resultFinal = (from e in result
                                   where 
    (from m in GetVeh
                                           select m
    .Make_ID).Contains(e.Make_ID)
                                   
    select e
                                  
    ).ToList();

                return 
    Json(resultFinalJsonRequestBehavior.AllowGet);

            } 
    Another weird thing that happens is, if I add the "!" in the following code, it does return the vehicle makes that do not exist in the vehicle details table but without the "!", it does not search for the values that do exist in both tables.

    PHP Code:
    var resultFinal = (from e in result
    where 
    !(from m in GetVeh 
    select m
    .Make_ID).Contains(e.Make_ID)
    select e
    ).ToList(); 

    Sample Data:
    1. BMW exists in Vehicle_Make(Make_ID PK) and Vehicle_Details(Make_ID FK).
    2. Vespa exists in Vehicle_Make but not in Vehicle_Details

    If I search for BMW it does not work but if I search for Vespa or any vehicle that does not exist in the Vehicle_Details table, it will find those, but not the vehicles that exist in both tables.

    Once I get the correct items that need to be searched for like BMW, it must match the Dealerships(Users table) and take the MapLat & MapLong from the "Users" table and then pin point them onto the map.

    Vehicle_Details has (User_ID FK) which is the PK of the Users Table.

    Basically, Vehicle_Details has Users and Vehicle_Make linked to it, while Users and Vehicle_Make do not have a link in them so therefore vehicle details is the link. Is this possible to make work for what I require?

    Thanks !

  2. #2
    Join Date
    Mar 2017
    Posts
    9

    Re: MVC List Not Working as it should

    Anybody have an idea how I can make this project work with my requirements?

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MVC List Not Working as it should

    Have you tried setting a breakpoint and stepping through the code?

    The first query looks suspect (unless it's a simple case of a misnamed variable). Should vehicle make be compared with location?

  4. #4
    Join Date
    Mar 2017
    Posts
    9

    Re: MVC List Not Working as it should

    The string is just called "Location". That won't affect anything bud. I called it location in the web page code as well and never change it as yet, but that's not the part with the issue. I'm just not sure how to make the query work.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MVC List Not Working as it should

    Quote Originally Posted by Vegeta ZA View Post
    The string is just called "Location". That won't affect anything bud. I called it location in the web page code as well and never change it as yet, but that's not the part with the issue. I'm just not sure how to make the query work.
    You have multiple places in your code where one or more problems could be. For example, in the first query, does it return any values? Is location (um, 'make name') getting set to a proper value? Is it empty or null? If result populates correctly, what about GetVeh and resultFinal?

    The bottom line is you're going to have to set some breakpoints and debug your code. If you don't know how to do this, let us know and we'll walk you through how to do it. Knowing how to debug is a great skill to have and will save you a bunch of time. Rather than guessing what a problem is, you can see the values and know what is going on.

  6. #6
    Join Date
    Mar 2017
    Posts
    9

    Re: MVC List Not Working as it should

    Quote Originally Posted by Arjay View Post
    You have multiple places in your code where one or more problems could be. For example, in the first query, does it return any values? Is location (um, 'make name') getting set to a proper value? Is it empty or null? If result populates correctly, what about GetVeh and resultFinal?

    The bottom line is you're going to have to set some breakpoints and debug your code. If you don't know how to do this, let us know and we'll walk you through how to do it. Knowing how to debug is a great skill to have and will save you a bunch of time. Rather than guessing what a problem is, you can see the values and know what is going on.
    1. Yes the "Location" does return all the make_name's that exist in vehicle_details table, I will change "Location" to vehicle_make soon but that doesn't affect anything now so I left it. It's just the string name for the method and I called it up in the index class so for now, since that's not the issue(I Hope :P), I will leave it till the main objective is fixed.

    2. Please walk me through how to debug this specific project, every time I do it, it loops through the same code over and over and I don't really know what's happening. I'm still a MVC noob.

    3. I think the issue comes from the other two queries because for some reason when I use the following code, it allows me to search for the vehicles that exist in Vehicle_Make but not Vehicle_Details so I am guessing that the link between the tables is what's broken:

    PHP Code:
    var resultFinal = (from e in result 
                                   where 
    !(from m in GetVeh 
                                           select m
    .Make_ID).Contains(e.Make_ID
                                   
    select e 
                                  
    ).ToList(); 
    When I have the "!", i am able to search for the vehicles that do not exist in both tables, but only the ones that exist in vehicle make, this makes sense because vehicle make has all the vehicles and vehicle details does not have all of these foreign keys(Make_ID). So what I do not get is, how come it is able to allow me to search for the vehicles that do not exist in both but only Vehicle_Make, and it returns the vehicle that I search for, but when I remove the "!" to search for the vehicles that do exist in both then it does not work. The search is just blank and the list disappears.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: MVC List Not Working as it should

    When running the code locally....

    To set a breakpoint, click on the line of code with the mouse and press F9. Press F5 to start debugging. Navigate through the app to the functionality you are trying to test (i.e., the search function in this case).

    Your breakpoint should hit. Use F10 to start stepping over each line of code. After a line of code has executed, hover over a variable to see its value. If the variable is a class or collection, you may have to expand its properties to see any interesting values.

    Doing this for the 3 queries in the code should tell you which query is not returning results. Adjust the queries as appropriate.

  8. #8
    Join Date
    Mar 2017
    Posts
    9

    Re: MVC List Not Working as it should

    Quote Originally Posted by Arjay View Post
    When running the code locally....

    To set a breakpoint, click on the line of code with the mouse and press F9. Press F5 to start debugging. Navigate through the app to the functionality you are trying to test (i.e., the search function in this case).

    Your breakpoint should hit. Use F10 to start stepping over each line of code. After a line of code has executed, hover over a variable to see its value. If the variable is a class or collection, you may have to expand its properties to see any interesting values.

    Doing this for the 3 queries in the code should tell you which query is not returning results. Adjust the queries as appropriate.
    I have managed to fix the code, and it is giving me better results. Just a few more things to sort out and then I would be proud of this code. Thanks a lot for your suggestions, I made a few changes in the db, used Linqer to covert my sql code to linq and also in the return statement at the end, I removed the resultFinal and just used GetVeh and it seems to be working better. Just need to make it work faster as well as do some other changes and then this will be working 100% to my requirements!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured