c# - Trying to get data shown in a drop down list -


i have tried lot of different solutions online, haven't been able find solutions implement solution looking correctly.

i want combine 2 attributes of table, these being firstname , lastname, , want display these coupled attributes dropdownlist user choose. i've tried using selectlistitem, has resulted in errors , unable compile.

so far, solution involves this:

in controller:

     var data =              p in db.contacts                    select new                     {                       firstname = p.firstname,                       lastname = p.lastname                     };         selectlist personlist = new selectlist(data, "firstname", "lastname"); 

in view:

@html.dropdownlist("contactname", (selectlist)viewbag.names) 

the output of drop down containing options in format:

{ firstname = joe, lastname = bloggs } 

any appreciated.

you need concatenate 2 field one.

var data =              p in db.contacts                    select new                     {                       name = p.firstname + " " + p.lastname,                       id = p.idcontact                    };         selectlist personlist = new selectlist(data, "id", "name"); 

with code above achieve need.

but read answer on how better work dropdownlists: https://stackoverflow.com/a/20008816/7720


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -