2013-02-08 68 views
0

我有一個名爲產品的集合,它有兩個字段Id和Name。Dropdownlist viewdata?

有關創建下拉列表中,我使用

ViewData["Geography"] = new SelectList(parent,"Id","Id"); 

我想在下拉列表中顯示編號+姓名。

我嘗試以下事情,因此未從parentCollection工作

ViewData["Geography"] = new SelectList(parentCollection,"Id","Id Name"); 

ViewData["Geography"] = new SelectList(parentCollection,"Id","Id" + "Name"); 

回答

0

查詢,並創建一個新的屬性與select new

ViewData["Geography"] = 
    new SelectList((from s in parentCollection select new { 
     ID = s.Id, 
     IdAndName = s.Id + " " + s.Name}), 
     "ID", 
     "IdAndName", 
     null);