1

I'd like to populate a dropdown list based on the selection chosen in the previous field.

Currently I have 2 Lists from my ViewModel like so:

Manufacturers = context.ManufacturersTable.OrderBy(x => x.ManufacturerName).ToList(),
Models = context.ModelsTable.OrderBy(x => x.ModelName).ToList(),

Both lists are populated using a SQL Table with their own data model.

In the Models table, I have a column for ManufacturerID which matches that of the ManufacturerID in the Manufacturers table.

I'd like to populate the Models list based on the selection of the Manufacturers list so that only models associated with the selected manufacturer are displayed.

How would I do this using Lambda?

I've been playing with Where and Select but haven't quite been able to get there.

EDIT:

Here's the tables

MANUFACTURER   |   MANUFACTURER_ID
Manufacturer 1     GUID1
Manufacturer 2     GUID2

MODEL   |    MODEL_ID    |    MANUFACTURER_ID
Model 1 | GUID1          |    GUID1
Model 2 | GUID2          |    GUID2
Model 3 | GUID3          |    GUID1
Model 4 | GUID4          |    GUID1
1
  • We need to see what columns are in both tables. You need to join the two tables by a common field. Commented Jul 8, 2019 at 9:55

2 Answers 2

1

When you change the selection of the manufacturer field, call a controller method with manufacturer id as a parameter that returns:

context.ModelsTable
    .Where(x => x.ManufacturerId == manufacturerId)
    .OrderBy(x => x.ModelName)
    .ToList();

...then update the select list with the result.

Sign up to request clarification or add additional context in comments.

Comments

0
context.ModelsTable.Where(x => x.ManufacturerId == value).OrderBy(x => x.ModelName).ToList()

This doesn't work? Pass ManufactureId as value. Where keyword filter data but returns all columns from the table. With Select you can select which columns you want to return. You can use these keywords together.

3 Comments

That's where I was going but getting the value was the problem.
You should create action in a controller which takes id as an argument and call it from frontend - you can use jQuery to do it. On select event from first dropdown call this new action. I don't understand what is exactly a problem for you. What is your approach - code first or database first?
Thanks, think I've got it. Got an issue with an Ajax call but I'll do a separate question for that.

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.