如果我有一个如下所示的数据库表:
| ID | FirstName | LastName |
_________________________________________
1 John Doe
2 Test Subject
我想创建一个全名下拉列表,我该怎么做呢?
var fullNameSelectList = new SelectList(db.Test, "ID", "FirstName"); // Gives me a ddl of only firstnames
var fullNameSelectList = new SelectList(db.Test, "ID", "FirstName" + "LastName"); // Gives me error, database doesn't contain property with name FirstNameLastName
有没有办法在SelectList重载中组合这些属性?
我知道我可以创建一个ViewModel,但我只想知道这是否可行。
感谢任何帮助。
答案 0 :(得分:2)
你可以试试这个
var fullNameSelectList = new SelectList(
db.Test.Select(x => new {x.ID, FullName = x.FirstName + " " + x.LastName}).ToArray(),
"ID", "FullName");