Search code examples
c#asp.net-coreasp.net-core-mvc

Getting an error message when running my ASP.NET Core MVC project


I'm been staring at this project for a few days now. I'm trying to move a console app to an ASP.NET Core MVC project. It's supposed to read a Json and display it on a page. I can get the response and write it in my console app but when I copy the code over it's not working.

This is the CardController:

using Final_Project.Models;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RestSharp;

namespace Final_Project.Controllers
{
    public class CardController : Controller
    {
        public ActionResult Card()
        {
            var client = new RestClient("https://pokemon-tcg-card-prices.p.rapidapi.com/card?name=Arcues%20V");

            var request = new RestRequest();

            request.AddHeader("X-RapidAPI-Key", "");
            request.AddHeader("X-RapidAPI-Host", "");

            var response = client.Execute(request).Content;

            //Console.WriteLine(response);

            Root cards = JsonConvert.DeserializeObject<Final_Project.Models.Root>(response);

            return View(cards);
        }
    }
}

It's supposed to deserialize it to my root class:

using static Final_Project.Models.CardInfo;

namespace Final_Project.Models
{
    public class Root
    {
        public List<Result>? cards { get; set; }
        public Paging? paging { get; set; }
    }
}

Then put it into the CardInfo class. It's just a small screenshot of everything that's in this class.

public class Result
{
    public string? cardId { get; set; }
    public string? name { get; set; }
    public string? cardNumber { get; set; }
    public string? setNumber { get; set; }
    public string? setId { get; set; }
    public string? set { get; set; }
    public string? series { get; set; }
    public string? variant { get; set; }
    public string? superType { get; set; }

    public List<string>? types { get; set; }
    public List<string>? subTypes { get; set; }

    public string? rarity { get; set; }

    public List<string>? pokemon { get; set; }

    public string? artist { get; set; }
    public MinSoldPrice? minSoldPrice { get; set; }
    public LowSoldPrice? lowSoldPrice { get; set; }
    public SoldPrice? soldPrice { get; set; }
    public HighSoldPrice? highSoldPrice { get; set; }
    public MaxSoldPrice? maxSoldPrice { get; set; }
    public int? soldVolume { get; set; }
    public DateTime? soldLastUpdatedAt { get; set; }
    public MinListingPrice? minListingPrice { get; set; }
    public LowListingPrice? lowListingPrice { get; set; }
    public ListingPrice? listingPrice { get; set; }
    public HighListingPrice? highListingPrice { get; set; }
    public MaxListingPrice? maxListingPrice { get; set; }
    public int? listingVolume { get; set; }
    public DateTime? listingLastUpdatedAt { get; set; }
    public Prices? prices { get; set; }
}

Then it should display the data in the Card view:

@model IEnumerable<Final_Project.Models.Root>

<h2>Card Info</h2>

<div> style="display: block">
    <a href="/Card/DispayCard/">Card Information</a>
</div>

<table class="table" >
    <tr>
        <th>Name</th>
        <th>Variant</th>
        <th>Card Number</th>
        <th>Rarity</th>
        <th>Card ID</th>

    </tr>

    @foreach (var product in @Model)
    {
        <tr>
            <td><a href=/CardInfo/ViewProduct/@product.name>@product.name</a></td>
            <td>@product.name</td>
            <td>@product.variant</td>
            <td>@product.cardNumber</td>
            <td>@product.rarity</td>
            <td>@product.cardId</td>
        </tr>
    }
</table>

When I run the program I get this error message:

RuntimeBinderException: Cannot implicitly convert type 'Final_Project.Models.Root' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

Any thoughts or ideas would greatly be appreciated.

I've tried to just move the variables from the Result class. I was able to get the dot notation to work but it still didn't display anything.


Solution

  • The problem is in a couple of lines of code:

    Root cards = JsonConvert.DeserializeObject<Final_Project.Models.Root>(response);
    

    You are getting a single result in the previous line. A single root.

    Yet in your view you expect an IEnumerable of root. Meaning many of them.

    @model IEnumerable<Final_Project.Models.Root>
    

    You need to change your view to accept a single root like this:

    @model Final_Project.Models.Root
    

    Then change your foreach like this:

    @foreach (var product in @Model.cards)