gsiradze
gsiradze

Reputation: 4733

populate multiple fields in SelectList

 ViewBag.partners = new SelectList(db.PRT_PARTNERS, "ID", "FIRST_NAME", "LAST_NAME");

db is my database object

PRT_PARTNERS is the table

ID is the field which must select

and FIRST_NAME is the field which will text in dropdownlist

can i populate it like this?

ViewBag.partners = new SelectList(db.PRT_PARTNERS, "ID", "FIRST_NAME + LAST_NAME");

Upvotes: 0

Views: 266

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

You can do but you have to project it in memory first using Select():

var partners = db.PRT_PARTNERS.Select(x=> 
                                   new 
                                      { 
                                        ID =x.ID,
                                        Name = x.First_Name +" "+ x.Last_Name
                                    });

ViewBag.partners = new SelectList(partners , "ID", "Name");

Upvotes: 1

Related Questions