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
Post a Comment