I'm new to MVC and I'm having a hard time figuring out how I can have a collection (enumeration) within a model.
I have a Model called API:
public class API
{
public int APIId { get; set; }
public int APICategoryId { get; set; }
public virtual APICategory APICategory { get; set; }
public string APIName { get; set; }
...
public Dictionary<int, bool> SupportedPlatforms { get; set; }
}
As you can see above, I want each API to have a list of supported platforms, which may be different per API.
On the controller, I am filling the dictionary with a Platform Ids as key and a boolean value as the value.
public ActionResult CreateAPI()
{
var model = new API();
model.SupportedPlatforms = new Dictionary<int, bool>();
var platformList = platforms.GetAll();
foreach (Platform p in platformList)
{
if (!model.SupportedPlatforms.ContainsKey(p.PlatformId))
{
model.SupportedPlatforms.Add(p.PlatformId, false);
}
}
return View(model);
}
The view simply has a checkbox generated for each Key-Value pair.
On debugging mode, I see that the Dictionary is properly enumerated accordingly to how the user selects the checkbox on the HttpPost
.
However, this dictionary is not "saving" to the database, and when I try to access SupportedPlatforms
elsewhere outside the post method, it returns null for me.
When I take a look at the Server Explorer, I do not even see a SupportedPlatforms column for the API table.
Is this just how the Entity Framework works (where enumerations are not saved on to the database)?
If this is the case, what would be the best way for modelling a similar list/map of booleans?
Thanks in advance.
edit:
I just wanted to be a bit more clear with the question:
How would I model the entities in the above situation?
In the database, I should have an expandable number of platforms, and the platforms should not be directly related to an API.
However, any API could have a list of multiple supported platforms, creating a one to many relationship.
Thanks again.