ASP.NET:从数据库中的值生成ListBoxItems
我想填充一个列表框,所以我必须定义一个 ListBoxItem 数组。 数据存储在数据库中。
这是我原来的方法:
public IEnumerable<String> GiveStudentsNormalAsString(int lesson, int class)
{
Lesson l = giveLesson(lesson);
Class c = GiveClass(l, class);
return (from s in c.Students
orderby s.Name, s.FirstName
select s.Name);
}
所以“班级”包含学生列表(导航属性)。 我想要做的不是返回字符串的 IEnumerable,而是返回 SelectListItems 的 IEnumerable。
我已经开始了:
public IEnumerable<SelectListItem> GiveStudentsNormalAsString(int lesson, int class)
{
Lesson l = giveLesson(lesson);
Class c = GiveClass(l, class);
return .Select(student => new SelectListItem
{
Text = ...
Value = ...
};
}
但是我必须写什么作为文本和值?例如,我希望将学生编号作为值,我该怎么做?
这是我的 edmx: EDMX1
I want to populate a listbox, so I have to define an array of ListBoxItem.
The data is stored in a database.
This is my original method:
public IEnumerable<String> GiveStudentsNormalAsString(int lesson, int class)
{
Lesson l = giveLesson(lesson);
Class c = GiveClass(l, class);
return (from s in c.Students
orderby s.Name, s.FirstName
select s.Name);
}
So "Class" contains a List of Students (a navigational property).
What I want to do is not returning an IEnumerable of Strings, but an IEnumerable of SelectListItems.
I started already:
public IEnumerable<SelectListItem> GiveStudentsNormalAsString(int lesson, int class)
{
Lesson l = giveLesson(lesson);
Class c = GiveClass(l, class);
return .Select(student => new SelectListItem
{
Text = ...
Value = ...
};
}
But what do I have to write as Text and Value? for example, I want the Student Number act as Value, how can I do that?
Here's my edmx:
EDMX1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需在构建
SelectListItem
时引用student
变量即可:当然,如果您愿意,也可以使用 LINQ 语法编写它:
You just refer to the
student
variable when building theSelectListItem
:Of course it can also be written using the LINQ syntax, if you prefer so: