Shihan Khan
Shihan Khan

Reputation: 2188

Having two values on SelectList() in asp.net

I'm trying to develop a website using asp.net mvc 4 & EF6 where I'm using dropdownlist to assign datas to my required textboxes. So far I've managed to assign data to two textboxes where one holds the value of the selected item of dropdownlist & one holds the text given for the selected item. But I need to add another value from the selected item. My codes are below,

Controller

ViewBag.Clients = new SelectList(db.ClientInfoes, "age", "fullname");    //This is given
ViewBag.Clients = new SelectList(db.ClientInfoes, "id", "age", "fullname");    //I want something like this

View

@Html.DropDownList("Clients", "Select...")

// Textboxes are populated automatically by using jQuery
@Html.TextBoxFor(a => a.Fullname, new { id = "clientName", @readonly = "readonly" })
@Html.TextBoxFor(a => a.Age, new { id = "clientAge", @readonly = "readonly" })

How can I assign more than one value in the SelectList? Any way I can do that would be fine. Need this help badly. Thanks.

Upvotes: 1

Views: 1475

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

It looks like you want to show name and age both in the dropdown text, in that case you need to project the result coming from database :

var ClientInfoes = db.ClientInfoes
                     .Select(x=>
                             new {
                                    id = x.id,
                                    name = x.fullname+","+x.age 
                                 });

ViewBag.Clients = new SelectList(ClientInfoes, "id", "name");

Upvotes: 2

Related Questions