How to display data coming from the controller as collection in a ViewData dynamically Highcharts

K.K.H

DATA IN THE CSHTML FILE after using Midhun Mundayadan suggestion but chart still not working

The data in the controller is collected from the SQL database correctly, also it does exist in the viewData in view Cshtml.

///Model

    public class CaseMetricsModel
{
    public int Year { get; set; }
    public int WeekNumber { get; set; }
    public int NumberOfCases { get; set; }
    public int Size { get; set; }

    public CaseMetricsModel(int year, int weeknr, int number, int size)
    {
        Year = year;
        WeekNumber = weeknr;
        NumberOfCases = number;
        Size = size;
    }
}

public class CaseMetricsList
{
    public List<Abnamro.Nfr.Fdda.Edis20.Web.Models.CaseMetricsModel> List { get; set; }

    public CaseMetricsList()
    {
        List = new List<Abnamro.Nfr.Fdda.Edis20.Web.Models.CaseMetricsModel>();
    }
}

///Controller

        public IActionResult About()
    {
        ViewData["Message"] = "Your application description page.";


        //fill the data for the chart
        //create a new, empty list
        Models.CaseMetricsList model = new Models.CaseMetricsList();

        //load the csv file into the model (list)
        SqlConnection sqlConn = new SqlConnection(Abnamro.Nfr.Fdda.Edis20.Common.Helper.GetConnectionString());
        using (SqlDataAdapter dataAdapter = new SqlDataAdapter("select top(25) DATEPART(year, InsertedDateTime) as yearnr, DATEPART(week, InsertedDateTime) as weeknr,  count(*) as numberofcases,  sum(sourcecasesize) as Size from tblcases where CaseStatusId <> 4 group by DATEPART(year, InsertedDateTime), DATEPART(week, InsertedDateTime) order by DATEPART(year, InsertedDateTime), DATEPART(week, InsertedDateTime) asc ", sqlConn))
        {
            // create the DataSet 
            DataSet dataSet = new DataSet();
            // fill the DataSet using our DataAdapter 
            dataAdapter.Fill(dataSet);
            DataTable table = dataSet.Tables[0];
           
            foreach (DataRow dr in table.Rows)
            {
                model.List.Add(new Models.CaseMetricsModel(Convert.ToInt32(dr["Yearnr"]),
                                                     Convert.ToInt32(dr["weeknr"].ToString()),
                                                     Convert.ToInt32(dr["numberofcases"]),
                                                     Convert.ToInt32(dr["Size"])));
            }


            ViewData["usaData"] = model;

        }

        return View();

    }

///View

   <script>
Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'New cases in Relativity per week'
    },
    xAxis: {
        type: 'category',
        categories: [
            @foreach(var item in @ViewData["usaData"] as IEnumerable<Abnamro.Nfr.Fdda.Edis20.Web.Models.CaseMetricsList>)
            {
                @item.List.Select(x => x.WeekNumber)
            }
        ],
        labels: {
            rotation: -45,
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    },
    yAxis: {
        min: 0,
        title: {
            text: '# cases'
        }
    },
    legend: {
        enabled: false
    },
    tooltip: {
        pointFormat: 'Number of new cases per week.'
    },
    series: [{
        name: 'Year/week',
        color: '#009286', // abnamro color
        data: @foreach(var item in @ViewData["usaData"] as IEnumerable<Abnamro.Nfr.Fdda.Edis20.Web.Models.CaseMetricsList>)
               {
                @item.List.Select(x => x.NumberOfCases)
               }
        dataLabels: {
            enabled: true,
            rotation: -90,
            color: '#FFFFFF',
            align: 'right',
            format: '{point.y:.1f}', // one decimal
            y: 10, // 10 pixels down from the top
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    }]
});
</script>

But the chart is not being displayed and the @foreach is returning an exception. How should the data in the view(using viewData or ViewBag) be displayed in order to display the graph correctly?. Thank you.and this my expected result.

Midhun Mundayadan

I think you should assign

ViewData["usaData"] = model.List;

And in view

@foreach(var item in @ViewData["usaData"] as IEnumerable<CaseMetricsModel>)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get data dynamically from one controller to another in angularjs?

Can't display data coming from service method into component

How to push data coming from setInterval into an array

How to display historical data and live data from database using Highcharts

How to display data coming from mongodb to reactjs after clicking the button?

How to display data coming from an API horizontally in react native

How to display the text with "anchor tag", coming from json response in react?

How to display No Data Available Message in highcharts

How to get calculated data from Controller to View and display it (laravel)?

How can i use a angular variable coming from controller in html?

How to pass viewData back to the controller from partial view?

How to display no data found in highcharts

Display in a tooltip data from a json in a Highcharts' pie

How to display monthly data in Highcharts graph?

how to Display data get from JSON in View controller

How to Display data from controller to full calander in code igniter

in script code how to get data from ViewData in cshtml

Angularjs how to Append an ng-repeat with data coming from a different controller

How to display data from mvc controller using angular2

how to display as it is data on html dropdown box coming from server?

how to display quilljs editor written data in Angular coming from Node API

How do i display data passed from viewbag in controller to view

How to display list of documents data from collection named by date?

Display data in the html coming from http service in json Angular

How to display specific data in combobox coming from database in c#?

Return ViewData from view to controller razor pages

How to Dynamically display this kind of data from an Array in PHP

Highcharts with JSON data coming from microcontroler

can I iterate on collection coming from controller to view in section script and get data of it in ASP.NET Core MVC

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive