NAHEEM OLANIYAN
NAHEEM OLANIYAN

Reputation: 153

Loop items in a list into ViewBag in Controller

I will like add all the items in the list into the ViewBag. I want something like:

ViewBag.Date = item1, item2, item3.....

My code only return one item (first index) in the list into the ViewBag. I want it separate because I want to use the item to plot a bar-chart in the view. I don't also mind to loop in the Razor View.

Controller

   public  ActionResult RimChart()
    {

        List<RimCalcs> listOfCategggories = null;

        Connectors aConn = Connectors.GetInstance();
        listOfCategggories = aConn.GetRimCalc();
        //var dates = listOfCategggories.Select(x => x.Date);
       // var profits = listOfCategggories.Select(y => y.Rprofit);
        ViewBag.list  = listOfCategggories;

        foreach (RimCalcs c in listOfCategggories)
        {

            ViewBag.Date = c.Date;
        }

        foreach (RimCalcs d in listOfCategggories)
        {

            ViewBag.profit = d.Rprofit;

        }
        return View();
    }

Razor View

@Html.Raw(@ViewBag.Date);

@using Newtonsoft.Json;

 @{
var profit = JsonConvert.SerializeObject(ViewBag.profit);
var date = JsonConvert.SerializeObject(ViewBag.Date);

}

<!DOCTYPE html>
 <html>
 <head>
 <meta name="viewport" content="width=device-width" />
<title>Charts</title>
<script  src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.bundle.min.js"></script>
<script>

        var barChartData =
            {
                labels: [@Html.Raw(date)], //the names displayed on the x-axis, see images below
            datasets: [{
                label: 'ProductWise Sales Count',
                backgroundColor: [
                    "#f990a7",
                    "#aad2ed",
                    "#9966FF",
                    "#99e5e5",
                    "#f7bd83",
                ],
                borderWidth: 2,
                data: [@profit]  //what you returned back from controller. values displayed on the y-axis, see images below
                }]
            };

                window.onload = function () {
                    var ctx1 = document.getElementById("barcanvas").getContext("2d");
                    window.myBar = new Chart(ctx1,
                        {
                            type: 'bar',
                            data: barChartData,
                            options:
                            {
                                title:
                                {
                                    display: true,
                                    text: "ProductWise Sales Count"
                                },
                                responsive: true,
                                maintainAspectRatio: true
                            }
                        });
                }
</script>
</head>
<body>
 <div style="text-align: center">
    <canvas id="barcanvas"></canvas>
 </div>
  <div style="text-align: center">
     Disclaimer:- This data is for demo it is
    not real data it wont relate to any company
 </div>
 </body>
 </html>  

Upvotes: 0

Views: 739

Answers (1)

Bryan Dellinger
Bryan Dellinger

Reputation: 5294

why not pass the whole listOfCategggories to the view. then just serialize the model in json. then you can just use the model as a JSON object in your script

controller

  public ActionResult RimChart()
        {
            return View(aConn.GetRimCalc());
        }

view

  @model List<RimCalcs> 
    @{
     var jsonData = @Html.Raw(Json.Serialize(@Model));
    }

<script>
// do your thing
</script>

Upvotes: 1

Related Questions