5

現在、IDを書き込み、フォームにFirstNameを表示するSelectListがあります。

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

FirstNameとLastNameをSelectListに連結する方法は?何かのようなもの:

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

1 に答える 1

8

次のようなものを試してください。

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);
    }
}
于 2013-02-04T12:50:07.750 に答える