Solved

MVC - combining two field in a drop down from joint table

Posted on 2014-11-14
1
127 Views
Last Modified: 2015-03-06
I have a table that has 2 foreign key. I would like to create in drop down that contain the value of these two foreign key. How
can I do that?


Here is my table definition:

Master Table
Id INT Prinary key
PhoneNumber VARCHAR(3)
IDLocation Foreign key to location table
IDDepartment Foreign Key to Department table

Master Table with data exammpe:
1|383-383-33838|1|1


Location Table
ID Primary key but this is related to the IDLocation Foreign key in Master table
Location Varchar

Location Table with example.
1|Chicago


Department Table
ID Primary key but this is relate to the ID Department foreng Key in Master table
Department varchar
1|Clothes

So in my drop down I would like  something like this.

Please choose
Chicago - Clothes
or "Value"  - "value"
0
Comment
Question by:yguyon28
1 Comment
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40444095
Set the following properties as shown below:

cmbBox.DataTextField = "Diaplay"           // for "Chicago - Clothes"
cmbBox.DataValueField = "BothFKs"       // for "1|1"

You could concatenate them in your Select statement:
Select 
   m.IDLocation + '|' + m.IDDepartment As BothFKs
   , l.Location + '-' + d.Department As Diaplay
   From [Master Table] m
   Inner Join [Location Table] l 
   on m.IDLocation = l.IDLocation
   Inner Join [Department Table] d 
   on m.IDDepartment = d.IDDepartment 

Open in new window


This will produce:

1|1 and  Chicago - Clothes
^-- value           ^--display

Most likely, you need to use CAST() like, apply it if it errors:

Cast(m.IDLocation as varchar(5)) + '|' + Cast(m.IDDepartment as varchar(5))  As BothFKs

cmbBox.SelectedItem.Text -->  "Chicago - Clothes"
cmbBox.SelectedItem.Value --> "1|1"
cmbBox.SelectedIndex --> 0 ,1, 2, ...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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