Search code examples
c#.netmodel-view-controllerentity

How do i connect my two models to allow cards to be entered into a deck?


I have 2 models, card and deck. User adds new card to collection (index page) and user can create a new deck. How do I work with my models to allow user to put a card from collection, to a deck?

How are my models looking? I assume that I need to bind card to deck etc, but not sure how to. Can someone clarify what I need to do and how to do it?

I've created 2 models deck and card, and made deck a data-type referenced in the card model.

public class Card
{
    public int Id { get; set; }
    public string Name { get; set;  }
    public string Attribute { get; set; }
    public int Level { get; set; }
    public string Type { get; set; }
    public int ATK { get; set; }
    public int DEF { get; set; }
    public Deck Deck {get; set;}
    public int Deck {get; set;}
}

public class Deck
{  
    public int DeckId {get; set;}
    public string DeckName {get; set;}  
}

This is a Yu-Gi-Oh deck builder.

Expected results are, card from collection is then inserted into deck. Imagine when looking at Deck index it will be populated with cards from collection the user selects.


Solution

  • EDIT:

    Add 'middle' entity

    public class CardsInDeck
    {  
        public int DeckId { get; set; }
        public int CardId { get; set; }
    }
    

    Add a collection of 'middle' into deck model

    public class Deck
    {  
        public ICollection<CardsInDeck> Cards { get; set; }
    }
    

    Add a collection of 'middle' into card model

    public class Deck
    {  
        public ICollection<CardsInDeck> Decks { get; set; }
    }
    

    Here you have basic many to many relation. Basically 2 one-to-many relation (Deck-Middle entity, Card-Middle entity).