Modifying SelectList items

本文关键字:items SelectList Modifying | 更新日期: 2023-09-27 18:12:03

我一直在与SelectList作斗争,并决定尝试一个简单的实验:

SelectList list = new SelectList(db.People.OrderBy(x => x.Last), "ID", "Last", sItem);
foreach (SelectListItem item in list)
{
  item.Text = item.Text.ToUpper();
}
return list;

到最后,检查列表显示项目未被修改。到底发生了什么事?显然这里漏掉了什么。

Modifying SelectList items

您可以使用linq投影来创建您的SelectList项:

var toupper = db.People.OrderBy(x => x.Last)
                       .Select(x => new
                           {   
                              Id = x.Id,  
                              Last = x.Last.ToUpper(), //Whatever you want to upper case
                              Whatever = x.Whatever
                              // Add other items you want to show in select list
                           });
SelectList list = new SelectList(toupper, "Id", "Last", sItem);
return list;