2013-02-04 63 views
1

目前我有SelectList,它會寫入ID,並在窗體上顯示FirstName。在SelectList中組合2個字段

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

如何將FirstName和LastName連接到SelectList中?喜歡的東西:

ViewBag.Person = new SelectList(db.Person, "ID", "FirstName & LastName"); 
+0

[可能重複(http://stackoverflow.com/問題/ 2758734 /如何-可以-I結合 - 兩個領域-IN-A-選擇列表的文本描述使用) –

回答

5

嘗試這樣:

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

或添加新的屬性,以您的Person模型

public string Fullname 
{ 
    get 
    { 
     return string.Format("{0} {1}", FirstName, LastName); 
    } 
} 
相關問題