在SelectList中组合2个字段

时间:2013-02-04 12:44:42

标签: asp.net-mvc selectlist

目前我有SelectList写入ID,并在表单上显示FirstName。

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

如何将FirstName和LastName连接到SelectList?类似的东西:

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

1 个答案:

答案 0 :(得分: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);
    }
}