2

I'm a bit confused when trying to implement C# and jquery to work together. I have a .cs file and a javascript document in the same solution/project. My c# function returns a list of strings, which I would like to append to some HTML data using Javascript. I am also using a webform for my HTML. Is it possible to return my list on the javascript side?

javascript

$(function () {
    $.ajax({
        type: "GET",
        url: "Test/GetListData", 
        dataType: "data: Array<any>" //Something like this?
    });

    //Can i return the list and use it here?
});

c# method

public List<string> GetListData()
{
    List<string> mylist = new List<string>();
    mylist.Add("test1");
    mylist.Add("test2");
    mylist.Add("test3");
    return mylist;
}
5
  • the jQuery $.ajax docs are very well detailed and even include examples Commented Nov 3, 2014 at 21:53
  • What kind of web-application is it? webforms or mvc? Commented Nov 3, 2014 at 22:06
  • @VenkataPanga Web forms Commented Nov 3, 2014 at 22:07
  • possible duplicate to stackoverflow.com/questions/17402242/… and stackoverflow.com/questions/19264373/… Commented Nov 3, 2014 at 22:14
  • @VenkataPanga Thanks for the advice. It turns out I had to go in an entirely different direction, see my answer below. Commented Nov 4, 2014 at 17:53

3 Answers 3

2

Pulled from this thread:

You can serialize that list into some nice tidy JSON like this:

using System.Web.Script.Serialization;
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(aList);
Sign up to request clarification or add additional context in comments.

3 Comments

So is it necessary to serialize my list to JSON in order to use the data in JQuery?
It'd be the easiest way to go about it. JSON objects are light and easily parsed by jQuery.
Thanks for the tip. See my updated answer below for how I solved my issue.
1

Well after careful research I realized that I was going about my problem the wrong way. I wanted to have C# communicate with javascript/html for a web form in the same project. Using a list was a bad idea as my javascript couldnt effectively see it unless I formatted it as one big JSON String. Here is the updated code that solved my problem, hopefully it helps someone down the line. [ComVisible(true)] allows the external window named test to see my c# function, and the $.parseJSON(myarray) was able to parse my JSON string into a usable array.

c#

[ComVisible(true)]
public string GetData()
{
    string test = "[{"Name": "test1"}, {"Name": test2"}, {"Name": "test3"}]";
    return test;
}

javascript

<script type="text/javascript">
            var test = window.external;
            var myarray = test.GetData();
            var obj = $.parseJSON(myarray);
            alert(obj[1].Name);
        }

   </script>

1 Comment

Did you mean: {"Name": "test2"}, instead of {"Name": test2"} ?
0

Try something like this (Note: I had a file already setup so the name is different):

   <script type="text/javascript">

        $(function ()
        {
            $.ajax({
                url: "WebService.asmx/GetListData",
                success: OnSuccess,
                error: OnError
            });

        });

        function OnSuccess(data)
        {
            for (var i = 1; i < data.all.length; i++)
            {
                alert(data.all[i].textContent);
            }
        }
        function OnError(data)
        {
            alert(data.statusText);
        }
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.