我想制作一些虚拟数据以在我的 asp.net mvc 3 视图中使用。以下代码是控制器的一部分,应该将数据传递给视图。
List<KeyValuePair<int, int>> dummyData = new List<KeyValuePair<int, int>>();
dummyData.Add(new KeyValuePair<int, int>(1,1));
dummyData.Add(new KeyValuePair<int, int>(1,2));
dummyData.Add(new KeyValuePair<int, int>(2,1));
dummyData.Add(new KeyValuePair<int, int>(3,1));
dummyData.Add(new KeyValuePair<int, int>(4,1));
dummyData.Add(new KeyValuePair<int, int>(4,2));
dummyData.Add(new KeyValuePair<int, int>(4,3));
dummyData.Add(new KeyValuePair<int, int>(4,4));
顾名思义,这是我的虚拟数据。这背后的想法是,第一个数字代表表格中的行号,第二个数字代表列号。我想以某种方式组合与同一行相关但具有不同列号的记录。为此,我选择使用二维数组:
int dummyCount = dummyData.Count;
List<KeyValuePair<int, int>>[,] dummyArray = new List<KeyValuePair<int, int>>[dummyCount,dummyCount];
int index1 = -1;
int index2 = 0;
for (int i = 0; i < dummyCount; i++)
{
int tempColNum = 1;
if (dummyData[i].Value != tempColNum)
{
dummyArray[index1, index2].Add(dummyData[i]);
index2++;
}
else
{
index1++;
index2 = 0;
dummyArray[index1, index2].Add(new KeyValuePair<int, int>(dummyData[i].Key, dummyData[i].Value));
}
}
但是当我到达这里时:dummyArray[index1, index2].Add(new KeyValuePair<int, int>(dummyData[i].Key, dummyData[i].Value));
我从标题中得到错误:Object reference not set to an instance of an object.
。最初我只尝试过dummyArray[index1, index2].Add(dummyData[i]);
,但得到了同样的错误。