多选列表构造函数

发布于 2024-12-27 19:46:54 字数 643 浏览 3 评论 0原文

根据 http://msdn.microsoft.com/en-us/library/dd470803 .aspx 中,MultiSelectList(IEnumerable, IEnumerable) 构造函数接受两个参数:itemsselectedValues

该文档并不完全明确,所以我只想澄清两点:

  1. selectedValues 到底如何工作?此构造函数是否仅迭代集合并为每个元素设置 .Selected = True
  2. selectedValues 必须是 items 的子集吗?这是如何精确定义的(即只要它们的 ToString 值匹配)?

具体来说,我正在使用 a jQuery 多选插件 并尝试基本上完成演示中所做的事情该插件(即“选定”列表在初始化时已由某些元素填充)。

According to http://msdn.microsoft.com/en-us/library/dd470803.aspx, the MultiSelectList(IEnumerable, IEnumerable) constructor takes in two parameters: items and selectedValues.

The documentation is not completely explicit so I just want to clarify two points:

  1. How exactly does selectedValues work? Does this constructor merely iterate through the collection and set .Selected = True for each element?
  2. Must selectedValues be a subset of items? How is this defined, precisely (i.e. as long as their ToString values match)?

Specifically, I am playing with a jQuery multiselect plugin and am trying to do essentially what is being done in the demo of that plugin (i.e. the "selected" list is already populated by certain elements upon initialization).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

三人与歌 2025-01-03 19:46:55

HTML:

<%=Html.ListBoxFor(model => model.tempCategories, (MultiSelectList)(ViewData["Categories"]), new {@size = "5" })%>

控制器代码:

 List<Categories> categoriesList = categories.Select();
 ViewData["Categories"] = GenCategoryMultiList(categoriesList);


private System.Web.Mvc.MultiSelectList GenCategoryMultiList(List<TemplateCategories> entity)
    {
        entity = entity.OrderBy(e => e.CategoryName).ToList();
        System.Web.Mvc.MultiSelectList selectList = new System.Web.Mvc.MultiSelectList(entity, "CategoryID", "CategoryName");
        return selectList;
    }

我将其与 JQuery Multiselect 一起使用。工作代码...

The HTML:

<%=Html.ListBoxFor(model => model.tempCategories, (MultiSelectList)(ViewData["Categories"]), new {@size = "5" })%>

Controller code:

 List<Categories> categoriesList = categories.Select();
 ViewData["Categories"] = GenCategoryMultiList(categoriesList);


private System.Web.Mvc.MultiSelectList GenCategoryMultiList(List<TemplateCategories> entity)
    {
        entity = entity.OrderBy(e => e.CategoryName).ToList();
        System.Web.Mvc.MultiSelectList selectList = new System.Web.Mvc.MultiSelectList(entity, "CategoryID", "CategoryName");
        return selectList;
    }

I am using this with JQuery Multiselect. Working code...

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