简体   繁体   English

如何在asp.net Core中将模型对象传递给javascript函数

[英]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). 我在将模型对象传递给.cshtml( asp.net核心项目)中的javascript函数时遇到问题。 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. 我使用ChartJs行样本构建一个Web应用程序。

web app js files web app js文件

There is a js function in .cshtml file, .cshtml文件中有一个js函数,

 <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. 我是asp.net core和js的新手。 If there is a way to add data from model to js function? 如果有办法将模型中的数据添加到js函数中? Thanks! 谢谢!

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! 使用var salesdata = @Html.Raw(ViewBag.Sales)从控制器获取数据!

use salesdata[i].salesprice to push data to the dataset! 使用salesdata[i].salesprice将数据推送到数据集!

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM