亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何同時發送兩個數組以使用 JSON asp.net MVC 查看

如何同時發送兩個數組以使用 JSON asp.net MVC 查看

臨摹微笑 2023-05-25 18:09:14
我必須根據用戶將輸入到我視圖中 beginform 中包含的輸入的值在我的視圖中創建一個動態圖表,但是它必須異步完成因此我使用 Ajax 和 Json 的原因,我發送用戶輸入很好地連接到控制器,然后使用該輸入我的代碼創建了兩個數組,一個字符串數組將用作圖表的標簽,另一個是用作圖表數據值的 int 數組。我的問題是我只能發送上面提到的那些數組之一,不能同時發送它們,我不確定如何完成,我在某處讀到我可以將數組作為集合發送,但我不是確定這是否正確??刂破髦械拇a(為了便于說明,我刪除了所有與問題無關的代碼并對其進行了簡化):        public ActionResult DoChart(string data)        {            string[] product = {"Bread", "Milk", "Eggs", "Butter"};            int[] quant = {10, 20, 30, 40};            return Json(product, JsonRequestBehavior.AllowGet);        }我視圖中的 Javascript 代碼:<script>    $(() => {        $("form#chartForm").on("submit", (e) => {            e.preventDefault();            let obj = {                quantity: $("#quantity").val()            };            $.ajax({                url: "@Url.Action("DoChart")",                method: "GET",                data: {                    data: JSON.stringify(obj)                },                success: (product, status) => {                    alert(product);                    var ctx = document.getElementById('myChart').getContext('2d');                    var myChart = new Chart(ctx, {                        type: 'bar',                        data: {                            labels: product,                            datasets: [{                                label: '# of Votes',                                data: [1,2,3,4,5,6,7,8],                                borderWidth: 1                            }]                        },                        options: {                            scales: {                                yAxes: [{                                    ticks: {                                        beginAtZero: true                                    }                                }]                            }                        }                    });                }            });        });    });</script>所以在我上面的代碼中,我發送了產品數組,然后為我的圖表設置標簽,但我也想發送量化數組并為我的圖表設置數據值。PS:我正在使用 Chart.Js 來創建我的圖表。任何幫助將非常感激。
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

最初,您需要一個支架來存放您的結果。例如,您可以創建一個如下所示的 holder 類


 public class MapResult

        {

            public string[] Products { get; set; }

            public int[] Quantity { get; set; }

        } 

控制器


您可以從控制器設置 MapResult 類的值,它有 2 個數組,一個用于產品,一個用于數量。


public ActionResult DoChart(string data)

        {

            string[] product = { "Bread", "Milk", "Eggs", "Butter" };

            int[] quant = { 10, 20, 30, 40 };


            var mapResult = new MapResult()

            {

                Products = product,

                Quantity = quant

            };


            return Json(mapResult, JsonRequestBehavior.AllowGet);

        }

AJAX 成功代碼


AJAX 結果包含兩個數組。您可以將它們添加到地圖中。


 success: (result, status) => {

                    alert(result.Products);

                    var ctx = document.getElementById('myChart').getContext('2d');

                    var myChart = new Chart(ctx, {

                        type: 'bar',

                        data: {

                            labels: result.Products,

                            datasets: [{

                                label: '# of Votes',

                                data: result.Quantity,

                                borderWidth: 1

                            }]

                        },

                        options: {

                            scales: {

                                yAxes: [{

                                    ticks: {

                                        beginAtZero: true

                                    }

                                }]

                            }

                        }

                    });


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號