I have a model with 3 properties, as I mentioned in a previous question (this is a different question of course), the model called “Something” and it has 3 properties:
int SomethingID
string Name
List<System.Drawing.Color>
(of course it’s not the original code, want to save some readers by saving some length)
And my Initializer is this:
protected override void Seed(DatabaseContext context)
{
var somethings = new List<Something>
{
new Something
{
Name="blah blah", Colors= { Color.Black, Color.Red }
}
};
somethings.ForEach(s => context.Somethings.Add(s));
context.SaveChanges();
base.Seed(context);
}
When I try to get those colors inside a view the colors are empty.
If I set a breakpoint inside the Initializer right after I constructed somethings, I get a property called Colors with a Count of 2 (so it’s fine, I have the object with my ID, name, and colors).
But when it’s inside the view, the debugger shows the Colors‘s count is 0, and as you can guess – the list is empty.
inside the View
Colors:
@foreach (var itemColor in item.Colors)
{
itemColor.ToString();
}
Desired result:
Colors: Black Red
Current result:
Colors: (empty)
You are just listing the colors in code block but not writing them to the screen. try
My favorite place to get quick reference for razor syntax
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
EDIT
Change your object to include this string property that will translate named colors to and from the database to your Colors List Property. This way your object is in full control of translating the information to the properties and you can continue using them as normal.