gpt4 book ai didi

c# - 在 Controller mvc 5 中迭代 IEnumerable ViewData

转载 作者:行者123 更新时间:2023-11-30 17:42:27 25 4
gpt4 key购买 nike

我正在尝试对 ViewData["element"] 进行 foreach 循环以构建一个数组

错误读取无法将对象完全转换为字符串[]

        foreach(var p in (IEnumerable<string>)ViewData["element"] as Array)
{
string[] workbookIDsLinks = p;
}

vuiewdat 来自

ViewData["element"] = names.Cast<XmlNode>().Select(e => e.Attributes["id"].Value);

任何帮助都会很棒

最佳答案

来自 MSDN :

if an array is cast to the type Array, the result is an object, not an array

因此,即使您明确要求一个 Array,您也得到了一个对象引用。 !

而不是存储 IEnumerable<string> (或 IEnumerable<anything> )在 ViewData ,只需在存储之前转换为数组或列表,方法是添加 ToList()在你的任务结束时。然后你可以更轻松地在另一端处理它。

对于您的情况,我不确定 names 的类型, 所以我不确定你需要做什么才能将你的结果变成 string 的列表.

关于c# - 在 Controller mvc 5 中迭代 IEnumerable ViewData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com