Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Add items to a list from view model

$
0
0

Hey how's it going,

I'm working on a deck builder for a card game. My rough build is up on https://chronoclashdeckbuilder.azurewebsites.net/DeckBuilder and I have the project on my github https://github.com/Zami77/ChronoClashDeckBuilder

The idea is to click the images that are on the deck builder page and have them be dynamically added to a list. Once the list is complete I could then have it be saved to my SQL database tied to a user account. I'm focusing just on one step at a time, and I'd like to be able to dynamically add and remove cards from the list and have them be displayed to the user. I'll post what I think are the relevant code sections, but if it feels something is missing please check out my github. Thanks in advance for any help!

DeckBuilder View Index.cshtml

@model ChronoClashDeckBuilder.Models.ViewModels.DeckBuilderListViewModel

@{
    ViewData["Title"] = "Deck Builder";
}<h1>Deck Builder!</h1><p>Still have to implement picking a card and adding to deck.</p><p><a class="badge-dark" asp-area="" asp-controller="DeckBuilder" asp-action="Index" asp-route-curDeck="@Model.NewDeck">Display Deck</a></p><tbody>
    @foreach (var item in Model.NewDeck)
    {<tr><td>
                @item.CardName</td></tr>
    }</tbody><tbody>
    @foreach (var item in Model.Cards)
    {<tr><td><input type="image" src="~/Images/Chrono Clash Card Images/Naruto Set 1/@item.CardImage"
                       alt="@item.CardName"
                       style="width:136px;height:185px;" />
                @*How to add from viewlist to razor page, check javascript
                asp-action="@model.newdeck.add(item)"*@</td></tr>
    }</tbody>

DeckBuilderController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChronoClashDeckBuilder.Models;
using Microsoft.AspNetCore.Mvc;

namespace ChronoClashDeckBuilder.Controllers
{
    public class DeckBuilderController : Controller
    {
        private ICardRepository repository;
        public DeckBuilderController(ICardRepository repo)
        {
            repository = repo;
        }
        public IActionResult Index(List<Card> curDeck, Card cardToAdd)
        {
            if (cardToAdd != null)
                curDeck.Add(cardToAdd);

            return View(new Models.ViewModels.DeckBuilderListViewModel
            {
                Cards = repository.Cards,
                NewDeck = curDeck
            });
        }
    }
}

Models Card.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace ChronoClashDeckBuilder.Models
{
    public partial class Card
    {
        [Key]
        [StringLength(20)]
        public string CardNumber { get; set; }
        [StringLength(20)]
        public string CardSet { get; set; }
        [StringLength(20)]
        public string CardType { get; set; }
        [StringLength(100)]
        public string CardImage { get; set; }
        [StringLength(10)]
        public string CardColor { get; set; }
        public int? CardCost { get; set; }
        public int? CardStrength { get; set; }
        [StringLength(80)]
        public string CardName { get; set; }
        [StringLength(200)]
        public string CardAbilities { get; set; }
        [StringLength(200)]
        public string CardStackAbilities { get; set; }
    }
}

ViewModels DeckBuilderListViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ChronoClashDeckBuilder.Models.ViewModels
{
    public class DeckBuilderListViewModel
    {
        public IEnumerable<Card> Cards { get; set; }
        public List<Card> NewDeck { get; set; }
    }
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>