【问题标题】:Sorting the HTML dropdown menu (SelectList) in ASP.NET, C#在 ASP.NET、C# 中对 HTML 下拉菜单 (SelectList) 进行排序
【发布时间】:2013-05-03 14:05:03
【问题描述】:

在 aspx 文件中,我有: = Html.DropDownList("SiteID", ViewData["Sites"] as SelectList)

在cs文件中我有:

ViewData["Sites"] = new SelectList(pr.GetUnassignedPortfolioSites(GetAuthenticatedContext(), id), "SiteID", "SiteName");

它可以工作,但是,我需要对 SelectList 进行排序,以便在选择下拉菜单时对其进行很好的排序。

另外,我需要从列表中选择一个项目,并使其显示为在单击下拉菜单之前可见的第一个项目。

非常感谢您的帮助!

PS 我已经尝试过查看其他示例,但没有运气。

【问题讨论】:

  • 你要默认选择哪一项?
  • 无论是来自“SiteID”还是“SiteName”,我都需要先在列表中搜索它。
  • 到目前为止有任何反馈吗?
  • 老兄,您应该就收到的答案提供反馈,例如投票或解释您还需要什么。

标签: c# asp.net sorting drop-down-menu selectlist


【解决方案1】:

我稍微改了一下,看来SelectList在设置默认选中项时有些问题。

看看这个:

@{
  List<SelectListItem> list = (List<SelectListItem>)ViewData["Sites"];
  list.Where(x => x.Value == "1").Single().Selected = true;
}
@Html.DropDownList("SiteID", list.OrderBy(x => x.Selected).ThenBy(x => x.Text))

还有控制器:

        List<SelectListItem> items = new List<SelectListItem>();

        items.Add(new SelectListItem { Text = "Action", Value = "0" });

        items.Add(new SelectListItem { Text = "Drama", Value = "1" });

        items.Add(new SelectListItem { Text = "Comedy", Value = "2" });

        items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" });

        ViewData["Sites"] = items;

【讨论】:

  • 我想报告一个问题的解决方案,但是别人拿来解决它,我没有时间看解决方案...谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多