locked
getting data from c# list into javascript RRS feed

  • Question

  • User-153404742 posted

    I want to pass list of strings and a list of integers to be used in a javascript function....currently, what works is the following:

    var rd = {

    Labels: ["14", "16", "18", "20"],

    Data: [1, 5, 9, 315],

    }

    I'm trying to assing Labels: array a list of string form c# code behind...but it's not working.  Using the following throw error:

    var rd= {
    Labels: Newtonsoft.Json.JsonConvert.SerializeObject(xValues),
    Data: Newtonsoft.Json.JsonConvert.SerializeObject(yValues),
    }

    xValues is a List<string> 

    yValues is a List<int>

    I tried using hiddenField and storing the string and then using JSON.stringify but it doesn't get the data....Any help in getting Labels as an array of string and Data in array of integer from code behind from list objects as defined above?

    When I pass into hiddenfield as 

    hdfLabels.Value = String.Join(",", xValues.ToArray());
    hdfData.Value = String.Join(",", yValues.ToArray()); I'm getting values like 1,3,5  but I need it to return ["1", "3", "5"] for the xValues array and [1,2,3] for the yValues.

    Monday, October 1, 2018 9:18 PM

Answers

  • User-153404742 posted

    working solution:

     var xValues = [];
        var yValues = [];
     
        var arr = $('#hdfLabels').val().split(",");
        for (var i = 0; i < arr.length; i++) {
            xValues.push(String(arr[i]));
        }
     
        var arr2 = $('#hdfData').val().split(",");
        for (var i = 0; i < arr2.length; i++) {
            yValues.push(arr2[i]);
     
     
        }
     
     
        var rawData = {
            Labels: xValues, /* Newtonsoft.Json.JsonConvert.SerializeObject(Labels),*/
            Data: yValues, /*Newtonsoft.Json.JsonConvert.SerializeObject(Data),*/
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 3, 2018 4:58 PM

All replies

  • User2053451246 posted

    Using the following throw error:
    And the error message is? ALWAYS post the exact error message.

    Tuesday, October 2, 2018 12:27 AM
  • User1724605321 posted

    Hi inkaln,

    Not sure about your requirement and problem , please refer to below code sample :

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">
            function getVlaue() {
    
                alert($('#HiddenField1').val()); //result:["str1","str2","str3"]
                alert($('#HiddenField2').val());  //result: [1,2,3]
             
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input id="Button1" type="button" value="button" onclick="getVlaue()" />
                <asp:HiddenField ID="HiddenField1" runat="server" />
                <asp:HiddenField ID="HiddenField2" runat="server" />
            </div>
        </form>
    </body>
    </html>

    Code behind :

                List<string> strList = new List<string>();
                strList.Add("str1");
                strList.Add("str2");
                strList.Add("str3");
    
                List<int> intList = new List<int>();
                intList.Add(1);
                intList.Add(2);
                intList.Add(3);
    
    
                HiddenField1.Value= Newtonsoft.Json.JsonConvert.SerializeObject(strList);
                HiddenField2.Value = Newtonsoft.Json.JsonConvert.SerializeObject(intList);

    Best Regards,

    Nan Yu

    Tuesday, October 2, 2018 6:36 AM
  • User-153404742 posted

    working solution:

     var xValues = [];
        var yValues = [];
     
        var arr = $('#hdfLabels').val().split(",");
        for (var i = 0; i < arr.length; i++) {
            xValues.push(String(arr[i]));
        }
     
        var arr2 = $('#hdfData').val().split(",");
        for (var i = 0; i < arr2.length; i++) {
            yValues.push(arr2[i]);
     
     
        }
     
     
        var rawData = {
            Labels: xValues, /* Newtonsoft.Json.JsonConvert.SerializeObject(Labels),*/
            Data: yValues, /*Newtonsoft.Json.JsonConvert.SerializeObject(Data),*/
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 3, 2018 4:58 PM