0

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.

JTY
  • 1,009
  • 7
  • 13
  • 1
    Entity Framework Model classes should never be used directly as ViewModel objects. – Dai Sep 21 '15 at 03:56
  • 1
    You should use a view model with a property `List SupportedPlatforms` where `PlatformViewModel` contains properties `int ID` and `bool IsSelected`. Your database needs a table for the relationships - containing `APIId` (FK to API table) and `PlatformId` (FK to Platform table) –  Sep 21 '15 at 03:59
  • @StephenMuecke Hello Stephen, I have added an edit to the original question after you have pointed the biggest part out. What would be the best way to create this relationship, where one APIId can have multiple PlatformId's? – JTY Sep 21 '15 at 04:25
  • Assuming you have added the APIRoles table, you probably want something like [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) with `PlatformVM` instead of `RoleVM` and `APIVM` instead of `UserVM` –  Sep 21 '15 at 04:32
  • @StephenMuecke Thanks, you have definitely lead me to the right direction. – JTY Sep 21 '15 at 04:39

1 Answers1

1

There are 2 points you need to notice in this situation.

First, don't use Entity Framework Models as a ViewModels directly. You should create a new ViewModel with the necessary properties and populate data on it.

However, this dictionary is not "saving" to the database

If you want to save your data to the database, you have to do it yourself. For example:

using (var context = new MyDbContext())
{
    // Do something with your data here

    // Call SaveChanges() to save your changes to the database.
    context.SaveChanges();
}
Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • Thanks for the answer. I just wanted to clear that on the HttpPost method, I am calling SaveChanges(). Sorry for not including the Post code on the original question. However, the question remains, all of the basic properties, such as strings or ints are being saved and committed to the database, but not the collection. Does EF not handle automatic bindings of code first collection properties, and will I need to create a separate relationship table with the controller doing that work? Thanks again! – JTY Sep 21 '15 at 04:29
  • It seems that your question has been solved. That's great :) – Triet Doan Sep 21 '15 at 06:21