Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I change certain fields in a dropdownlist for every time a certain ID is chosen? For example I have this application that will map certain fields for a certain userID e.g user: 1 will have fields a + b. This will work for just displaying the fields but the dropdownlist will still display all fields e.g user: 1 will see fields a + b + c but is only required to see a + b. how can I make the dropdownlist display the correct fields. Here is what I have got so far:
VB
'In the controller
Dim userModel = New IssueTracker.ClientUserProjectIssue()

userModel.cTable = dbServer.ClientTables.Where(Function(x) x.userID = getuserID).ToList()

userModel.proTable = dbServer.ProjectTypes.Where(Function(x) x.ProjectTypeID = 1).ToList()

Return View(userModel)

'In the view
'Here I want to display a dropdownlist for each chosen ID

@For Each test In Model.cTable
          @Html.DropDownList("ProjectTypeID", New SelectList(Model.proTable.Where(Function(x) x.ProjectTypeID = test.ProjectTypeID), "ProjectTypeID", "ProjectTypeName"), "Please Select")
Next        

This is nearly working but instead of displaying the items in one dropdownlist it is displaying each item in one dropdownlist because of the dropdownlist. e.g user1: will see a dropdownlist that has field a, and another dropdownlist that has field b.

How can I fix this so it is all in the one dropdownlist?
Posted
Updated 28-Jul-15 22:07pm
v3

1 solution

The reason is because the markup you have is rendering one drop down list for each item in the Model.cTable items.
1. What should do is adhere to basic MVC approach of implementing a model class which is of a 'flat' structure and contains ONE list collection.
e.g.
Public Class MyDropDownListItems
{
public List<string> Items {get;set;}
public MyDropDownListItems()
{
Items = new List<string>();
}
}
2. Then, in the controller, populate that list collection model object with the data from both tables into one list:
var dropDownItems = new MyDropDownListItems();
//--> Add items here using your linq code in your question
3. Then you must modify the markup to bind to the model class object type rather than database linq items.
@Html.DropDownList("ProjectTypeID", New SelectList(Model.Items, "Please Select"))
 
Share this answer
 
Comments
Ciaran82 30-Jul-15 10:56am    
I understand that thanks, but I am having a bit of trouble thinking of the logic to get my dropdownlist to work. I have multiple users who can have multiple projectTypes so I assume I need a many to many table to make this work how can I logically (I don't need the code) create a dropdownlist for User No1 who has ProjectType1 and ProjectType2?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900