Somethingオブジェクトのコンストラクタで、新しいオブジェクトColorsをインスタンス化していることを確認してください。私の推測はあなたの問題です。あなたは多くのコードを投稿することはありませんでした。
このようなもの:
public Something()
{
this.Colors = new List();
}
このようにすると、Somethingオブジェクト内に常に有効なリストオブジェクトが存在します。
あなたのモデルコードを次のように変更してください:
namespace MVCApplication7
{
public class Something
{
public int SomethingID { get; set; }
public string Name { get; set; }
public List Colors { get; set; }
public Something()
{
this.Colors = new List();
}
}
}
これにより、新しい何かオブジェクトを作成するたびに新しい色のリストがインスタンス化され、オブジェクト参照エラーが防止されます。
UPDATE
Ok with what I have listed above as your model, here is your solution to the original question:
var list = new List()
{
new Something(){SomethingID = 1,Name="John", Colors = {Color.Red,Color.Black}},
new Something(){SomethingID = 2,Name="George", Colors = {Color.Bisque,Color.Blue}},
new Something(){SomethingID = 3,Name="Chris", Colors ={Color.Khaki,Color.Cornsilk}}
};
foreach (var item in list)
{
Console.WriteLine(item.Name);
foreach (var color in item.Colors)
{
Console.WriteLine(color.ToString());
}
Console.WriteLine("");
}
Somethingオブジェクトにはそれぞれ固有の色のリストがあることがわかります。
これがあなたの問題を解決する場合は、これを答えとして記入してください。