Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass model object to javascript function in asp.net Core

i have a problem in passing model objects to a javascript function in .cshtml(asp.net Core project). I have done a lot of search,but can't find a solution to solve this problem. I build a web application use ChartJs line sample.

web app js files

There is a js function in .cshtml file,

<script>       
        document.getElementById('addData').addEventListener('click', function() {
            if (config.data.datasets.length > 0) {
                var month = MONTHS[config.data.labels.length % MONTHS.length];
                config.data.labels.push(month);
                config.data.datasets.forEach(function(dataset) {
                    dataset.data.push(randomScalingFactor());
                });

                window.myLine.update();
            }
        });
    </script>

I'm new to asp.net core and js. If there is a way to add data from model to js function? Thanks!

like image 559
Jianchen Ding Avatar asked Jul 03 '17 08:07

Jianchen Ding


1 Answers

I have solved this problem!

Solution:

Model:

public class SalesViewModel
    {
        public string salesdate { get; set; }
        public int salesprice { get; set; }
    }

Data:

public class SalesContext
    {
        public string ConnectionString { get; set; }

        public SalesContext(string connectionString)
        {
            this.ConnectionString = connectionString;
        }
        public SalesContext()
        {          
        }

        public List<SalesViewModel> GetAllData()
        {
            List<SalesViewModel> list = new List<SalesViewModel>();
            list.Add(new SalesViewModel() { salesdate = "1", salesprice = 3 });
            list.Add(new SalesViewModel() { salesdate = "2", salesprice = 6 });
            list.Add(new SalesViewModel() { salesdate = "3", salesprice = 7 });
            list.Add(new SalesViewModel() { salesdate = "4", salesprice = 2 });
            list.Add(new SalesViewModel() { salesdate = "5", salesprice = 1 });

            return list;
        }
    }

Controller:

using Newtonsoft.Json;
public IActionResult Chart()
        {
            SalesContext sc = new SalesContext();
            string json = JsonConvert.SerializeObject(sc.GetAllData());
            //ViewData["chart"] = json;
            ViewBag.Sales = json;
            return View(sc.GetAllData());
        }

View:

document.getElementById('addData').addEventListener('click', function() {
            if (config.data.datasets.length > 0) {
                var salesdata = @Html.Raw(ViewBag.Sales);
                config.data.datasets.forEach(function(dataset) {
                        for (var i = 0; i < salesdata.length; i++) {
                            var month = MONTHS[config.data.labels.length % MONTHS.length];
                            config.data.labels.push(month);
                            dataset.data.push(salesdata[i].salesprice);
                        }
                });

                window.myLine.update();
            }
        });

use var salesdata = @Html.Raw(ViewBag.Sales) to get data from controller!

use salesdata[i].salesprice to push data to the dataset!

Thanks!

like image 164
Jianchen Ding Avatar answered Sep 21 '22 00:09

Jianchen Ding