【问题标题】:The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed当允许多选时,参数“表达式”必须计算为 IEnumerable
【发布时间】:2019-04-11 02:11:16
【问题描述】:

我正在尝试将 MultiSelectList 返回到视图,同时让所选内容与当前存储的 id 匹配。

问题是我得到了上面的错误,过去几个小时试图解决它,但没有运气,所以寻求帮助。

在我的课上我有

public MultiSelectList ListOfNaceCodesUpdate { get; set; }

我有控制器

 List<string> codeIds = new List<string>();

                //Get current list of Ids in database
                foreach (var item in model.NaceCodes.Split('\n'))
                {
                    codeIds.Add(item);
                }

                List<string> arrayNaceToString = codeIds;

                var cmsGenericText = UmbracoAssignedContentHelperClass.PageContentByAlias("introduction");

                var returnNaceCodes = cmsGenericText.GetPropertyValue<IEnumerable<IPublishedContent>>("displayNaceCodes");

                List<GenericContent.TextValueViewModel> naceCodes = new List<GenericContent.TextValueViewModel>();
                //Get full list of codes
                foreach (var data in returnNaceCodes.ToContentSet())
                {
                    naceCodes.Add(new GenericContent.TextValueViewModel(text: data.GetPropertyValue<string>("category", string.Empty), value: data.GetPropertyValue<string>("code", string.Empty)));
                }

                MultiSelectList list = new MultiSelectList(naceCodes, "Text","Value",arrayNaceToString.ToArray());

                model.ListOfNaceCodesUpdate = list;

在我看来

@Html.ListBoxFor(x => x.NaceCodes, new MultiSelectList(Model.ListOfNaceCodesUpdate,"Value","Text"), new { @class = "form-control", multiple = "multiple" })

但我不断得到:

参数“表达式”必须在以下情况下计算为 IEnumerable 允许多选。

我看过ASP.NET, MVC ListBoxFor Edit "The parameter 'expression' must evaluate to an IEnumerable when multiple selection is allowed."

SelectedValues not working in MultiSelectList mvc

How can I create a SelectList with multiple selected values?

但无法解决

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    这很有意义。当前选择的项目(或项目)必须从expression 中读取。为了便于进行多项选择,expression 必须返回一个IEnumerable,以便可以读取已经从中选择的多个项目。

    您的NaceCodes 属性似乎只是一个字符串,其中使用了某种任意分隔符。您是否可以将其改为某种类型的IEnumerable - 例如List&lt;String&gt;String[]?

    或者,您不能将其与属性直接相关,而是使用ListBox() 而不是ListBoxFor()。这可能不太可取,因为您可能希望稍后发布页面时将所选元素正确绑定到模型,而IEnumerable 属性可能是最简单的选项。

    【讨论】:

    • Hi moreON,它返回一个列表,请看图片添加
    • @GeorgePhillipson - 模型的NaceCodes 属性,在您的代码示例中用作model.NaceCodes.Split('\n')?对我来说,它看起来很可疑。
    • 嗨 moreOn,我很困惑,我返回一个 List arrayNaceToString = codeIds;,这是来自 model.NaceCodes.Split('\n')
    • 我需要休息一下,应该做完 public IEnumerable NaceCodeList { get;set; } 解决了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多