如何在 MVC 3 中将项目插入到填充列表的第一个索引?

发布于 2024-12-11 17:52:07 字数 1272 浏览 0 评论 0原文

我有一个由我填写并排序的清单!我想将一些新项目添加到该排序列表中,但这些新项目应该位于列表的顶部。

我正在将项目添加到列表中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜尕妞 2024-12-18 17:52:07

为什么不调用 Insert 方法?

why don't you invoke the Insert method ?

素食主义者 2024-12-18 17:52:07

创建一个新列表并在开始时添加所需的项目,然后添加您之前排序的其他项目。如果我没有完全误解你的话,我会工作的。

NewList.Add(the new ones)

foreach(var item in SupplierList){
NewList.Add(item);
}

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.

NewList.Add(the new ones)

foreach(var item in SupplierList){
NewList.Add(item);
}
故事与诗 2024-12-18 17:52:07

我想到的第一件事就是准备一份包含所有这些项目的清单。
因此,您可以对新项目使用 MyList.Insert(0)
然后我将生成与此类似的选择列表:

var select = new SelectLis(MyList, "Id", "Text", Model.SelectedValueId)

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:

var select = new SelectLis(MyList, "Id", "Text", Model.SelectedValueId)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文