如何在 MVC 3 中将项目插入到填充列表的第一个索引?
我有一个由我填写并排序的清单!我想将一些新项目添加到该排序列表中,但这些新项目应该位于列表的顶部。
我正在将项目添加到列表中:
var OnlySup = dataContext.GroupDetails.Where(c => c.sup_id == c.suppliers.supplier_id).Select(c=>c.sup_id).Distinct().ToList();
/* GET SUPPLIERS FROM DB */
foreach (var supp in OnlySup)
{
/* ADD EACH SUPPLIER TO LIST */
SupplierList.Add(new SelectListItem() { Text = supp.supplier, Value = "S" + supp.supplier_id.ToString() });
}
排序:
/* ReOrder The List */
SupplierList = SupplierList.OrderBy(c => c.Text).ToList();
列出我想要添加到列表顶部的项目:
/* DEFAULT ADD */
SupplierList.Add(new SelectListItem() { Text = "ALL Groups", Value = "G" });
SupplierList.Add(new SelectListItem() { Text = "ALL Suppliers", Value = "S" });
SupplierList.Add(new SelectListItem() { Text = "ALL Suppliers AND Groups", Value = "%" });
/* GET GROUPS FROM DB */
var OnlyGroup = dataContext.groups.OrderBy(c => c.id).Distinct().ToList();
/* FOR EACH GROUP */
foreach (var groups in OnlyGroup)
{
/* ADD EACH GROUP TO LIST */
SupplierList.Add(new SelectListItem() { Text = groups.group_name, Value = "G" + groups.id.ToString() });
}
那么我怎样才能实现这一目标?
I ve a list which is filled by me and sorted! I wanna add some new items to that sorted list but these new items should be at the top of the list.
I'm adding items to list:
var OnlySup = dataContext.GroupDetails.Where(c => c.sup_id == c.suppliers.supplier_id).Select(c=>c.sup_id).Distinct().ToList();
/* GET SUPPLIERS FROM DB */
foreach (var supp in OnlySup)
{
/* ADD EACH SUPPLIER TO LIST */
SupplierList.Add(new SelectListItem() { Text = supp.supplier, Value = "S" + supp.supplier_id.ToString() });
}
Sorting:
/* ReOrder The List */
SupplierList = SupplierList.OrderBy(c => c.Text).ToList();
List items that i wanna add to the top of the list:
/* DEFAULT ADD */
SupplierList.Add(new SelectListItem() { Text = "ALL Groups", Value = "G" });
SupplierList.Add(new SelectListItem() { Text = "ALL Suppliers", Value = "S" });
SupplierList.Add(new SelectListItem() { Text = "ALL Suppliers AND Groups", Value = "%" });
/* GET GROUPS FROM DB */
var OnlyGroup = dataContext.groups.OrderBy(c => c.id).Distinct().ToList();
/* FOR EACH GROUP */
foreach (var groups in OnlyGroup)
{
/* ADD EACH GROUP TO LIST */
SupplierList.Add(new SelectListItem() { Text = groups.group_name, Value = "G" + groups.id.ToString() });
}
So how can i achive this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不调用 Insert 方法?
why don't you invoke the Insert method ?
创建一个新列表并在开始时添加所需的项目,然后添加您之前排序的其他项目。如果我没有完全误解你的话,我会工作的。
Create a new list and add the items you want at the start, then add the other items that you sorted earlier. Sould do work if I didn't misunderstand you completely.
我想到的第一件事就是准备一份包含所有这些项目的清单。
因此,您可以对新项目使用
MyList.Insert(0)
。然后我将生成与此类似的选择列表:
The first thing that came to my mind, would be to prepare a list with all these items.
So you can use
MyList.Insert(0)
for your new Items.then I will generate the select list with something similar to this: