目前我有SelectList写入ID,并在表单上显示FirstName。
ViewBag.Person = new SelectList(db.Person, "ID", "FirstName");
如何将FirstName和LastName连接到SelectList?类似的东西:
ViewBag.Person = new SelectList(db.Person, "ID", "FirstName & LastName");
答案 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);
}
}