Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine 2 fields in SelectList

Currently I have SelectList that writes ID, and shows FirstName on the form.

ViewBag.Person = new SelectList(db.Person, "ID", "FirstName");

How to concatenate FirstName and LastName into SelectList? Something like:

ViewBag.Person = new SelectList(db.Person, "ID", "FirstName & LastName");
like image 362
Иван Бишевац Avatar asked Feb 04 '13 12:02

Иван Бишевац


1 Answers

Try something like this:

ViewBag.Person = 
new SelectList((from s in db.Person select new { 
    ID=s.ID,
    FullName = s.FirstName+ " " + s.LastName}), 
    "ID", 
    "FullName", 
    null);

Or Add a new property to your Person model

public string Fullname 
{
    get 
    {
        return string.Format("{0} {1}", FirstName, LastName);
    }
}
like image 141
AliRıza Adıyahşi Avatar answered Sep 17 '22 18:09

AliRıza Adıyahşi