Carlove
Carlove

Reputation: 449

How to use <list> with modelView

I'm trying to Graph Excel data using ChartJS. Visual Studio is saying that List<Graphs> does not contain a definition for Answers.

I can't find anything wrong with my code, though. Though, I've only been using VS for the past two days.

Can someone look at my code and maybe find a mistake, or two? Thanks!

ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using ReinovaGrafieken.Models;


namespace ReinovaGrafieken.Models
{
    public class GraphDataViewModel
    {
        public List<Graphs> GraphData { get; set; }
    }
}

Graphs Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ReinovaGrafieken.Models;

namespace ReinovaGrafieken.Models
{
    public class Graphs
    {
        public string Names { get; set; }
        public string AnswerHeaders { get; set; }
        public int Answers { get; set; }
        public string Questions { get; set; }
        public string AnteOrPost { get; set; }
    }
}

And a piece of the code from the View:

@model ReinovaGrafieken.Models.GraphDataViewModel
@{
ViewBag.Title = "Dashboard";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")

<h2>Dashboard</h2>

<div id="chart_container">
    <canvas id="bar_chart"></canvas>

    <script>
        var answers = @Html.Raw(Json.Encode(@Model.GraphData.Answers));
        var labels = @Html.Raw(Json.Encode(@Model.GraphData.Names));

This is where I got the ideas from, where it does work for that person: https://www.youtube.com/watch?v=E7Voso411Vs&t=2787s

Upvotes: 0

Views: 72

Answers (2)

Maksym Labutin
Maksym Labutin

Reputation: 571

Like @Stephen Muecke wrote, List<T> does not contain a property named Answers. You could take element, which you need like this:

@Model.GraphData.First().Answers

(get first element from the list). Or you can use .Foreach() method

Upvotes: 1

rahulaga-msft
rahulaga-msft

Reputation: 4144

GraphData is collection of Graph. So Answers is accessible property on Graph and not GraphData.

Upvotes: 1

Related Questions