如何在MVC 3中追加更多字段来选择列表项“Text”?

时间:2013-07-12 11:52:52

标签: asp.net-mvc asp.net-mvc-3 linq linq-to-sql linq-to-entities

我有:

ViewBag.StdLibraryId = new SelectList(db.StdLibrary, "Id", "Name",stdlibraryitem.StdLibraryId);

我想将另一列的值附加到文本输出中,因此它类似于

ViewBag.StdLibraryId = new SelectList(db.StdLibrary, "Id", Model.Name + "(" + model.Category.Name + ")", stdlibraryitem.StdLibraryId);

然而,显然上面的代码是错误的。我也倾向于在LINQ中使用Lambda表达式。

代码应该是什么样的?

提前非常感谢。

修改

我的新代码:

            ViewBag.StdLibraryId = new SelectList(db.StdLibrary.Select(r=> new {Id = r.Id, Name= r.Name + "(" + r.StdSection.Name + ")"}), "Id", "Name", stdlibraryitem.StdLibraryId);

1 个答案:

答案 0 :(得分:1)

您可以使用匿名对象进行连接。

var stdLibraryForCombo = db.StdLibrary
                         .Select(m => new {
                            Id = m.Id,
                            Name = m.Name + " (" + m.Category.Name + ")"
                         });
ViewBag.StdLibraryId = new SelectList(stdLibraryForCombo, "Id", "Name", stdlibraryitem.StdLibraryId);