JadeSerys
JadeSerys

Reputation: 127

How can I create a relationship between objects within the same table in mvc?

I have a table for cards with a lot of columns. One of those columns I have named "LinkedTo". In my view I want to be able to have a button that says "Flip Over" which will show the card information on the back of the card (as it would be in real life). The reason I don't want to put the information for the front and back in the same record is for search capabilities and the way I want the details view to appear. In my edit view, I would like to show a dropdown list that displays only cards with the "Flip" column marked as true (to make the list much shorter). On save, the CardID from that dropdown list would be saved to the "LinkedTo" column. Then I would want to be able to call that CardID in a link/button on the details view as mentioned above. As it stands, anyway I try to create a dropdown list I get ambiguity errors or nothing shows. If my model already knows all cards, how do I make it show a filtered list of the cards when editing one specific card? Is there an easier way to do this?

I tried this in my controller:

ViewBag.Card_ID = new SelectList(db.Cards.Where(w => w.Flip == true), "CardID", "Title");

And this in my view:

@Html.DropDownListFor(model => model.Cards.CardID, ViewBag.CardID as SelectList)

This is my Edit ViewModel:

public class EditCardViewModel
    {

        public Card Cards { get; set; }
        public HttpPostedFileBase ImageUpload { get; set; }
        public int[] LinkedCard { get; set; }
        public IEnumerable<SelectListItem> Abilities { get; set; }
        public int[] SelectedAbilities { get; set; }
        public IEnumerable<SelectListItem> Rarities { get; set; }
        public int SelectedRarities { get; set; }
        public IEnumerable<SelectListItem> MainTypes { get; set; }
        public int SelectedMainTypes { get; set; }
        public IEnumerable<SelectListItem> SubTypes { get; set; }
        public int SelectedSubTypes { get; set; }
        public IEnumerable<SelectList> CardSets { get; set; }
        public int SelectedCardSets { get; set; }
        public Rarity Rarity { get; set; }
        public MainType MainType { get; set; }
        public SubType SubType { get; set; }
        public CardSet CardSet { get; set; }

        public EditCardViewModel() { }
        public EditCardViewModel(Card card)
        {
            Cards = card;

        }
}

Any assistance would be very appreciated. :)

Upvotes: 0

Views: 48

Answers (1)

Kevin
Kevin

Reputation: 759

On POST the DropDownListFor section will bind the result to the field you tell it to. Right now you have that set to model.Cards.CardId. If you want that selection to be bound to the "LinkedTo" property then change it to that instead. Also I notice that you are passing in "ViewBag.CardId" instead of "ViewBag.Card_ID" that you show as the variable being set with the list.

@Html.DropDownListFor(model => model.Cards.LinkedTo, ViewBag.Card_ID as SelectList)

Here is a demo

Upvotes: 1

Related Questions