如何访问TempData的数组元素 [英] how to access an array element from tempdata
问题描述
我的问题在本质上是相当简单的,但我发现没有答案的StackExchange或谷歌的任何地方迄今。
My question is fairly simple in nature, but I've found no answers anywhere on StackExchange or Google thus far.
这里的情况是:
我有一个ASP.NET MVC 5项目中,我需要布尔值的数组传递给视图。数组大小设置为在客户记录项的长度。例如:
I have an ASP.NET MVC 5 project where I need to pass an array of boolean values to a view. The array size is set to the length of items on a customer record. For example:
var count = Customer.Items.Count;
int index = 0;
bool[] allowRenew = new bool[count];
然后,我通过一个foreach循环通过这样的更新后的数组:
I then update the array after passing through a foreach loop like this:
foreach (var item in Customer.Items)
{
if(conditionA)
{
allowRenew[index] = false;
}
else
{
allowRenew[index] = true;
}
index += index;
}
最后,我指定数组TempData的,所以我可以从我的观点访问它,因为这样的:
Finally, I'm assigning the array to TempData so I can access it from my view, as such:
TempData["allowRenew"] = allowRenew;
所以这里的问题,我如何可以访问我的看法数组中的元素?我打算使用foreach循环生成标记和访问基于关递增的索引数组元素。
So here is the issue, How can I access the array elements from my view? I was planning on using a foreach loop to generate markup and access the array elements based off an index that increments.
通常情况下,你会像这样访问数组元素:
Generally, you'd access array elements like this:
array[1]
或
array[2]
但自从我使用TempData的我不知道如何引用数组元素。例如:
But since I'm using TempData I don't know how to reference the array element. Example:
TempData["allowRenew"][0]
或
TempData["allowRenew"][1]
显然不起作用。
任何建议,具有很强的AP preciated。
Any suggestions are highly appreciated.
感谢您。
推荐答案
它实际上是pretty方便;只需在您的视图为它创建一个变量:
It's actually pretty easy; just create a variable for it in your view:
bool[] allowRenew = (bool[])TempData["allowRenew"];
这篇关于如何访问TempData的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!