问题
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");
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/14687082/combine-2-fields-in-selectlist