0

I'm very new to Angular, and I'm trying to implement it in my .NET MVC application but I've been stumped on how to send data from my C#. In my previous applications, I've used SignalR for all of my client-server communication and I can just return objects seamlessly from server to client without stringifying, serialising, or deserialising.. I presume with Angular it's not that easy? For example, I have server-side methods:

    [HttpGet]
    public int TestInt() { return 42; }

    [HttpGet]
    public string TestString() { return "hooray"; }

    [HttpGet]
    public List<int> TestList() { return new List<int> {0, 1, 2, 3, 4};}

and then in my Angular controller I have the a function that uses $http.get to call these methods

 $http.get('/UserRequest/TestInt').success(function (data) { var x = data; });

 $http.get('/UserRequest/TestString').success(function (data) {var y = data; });

 $http.get('/UserRequest/TestList').success(function (data) {var z = data;});

Now the first two work kinda as expected (though TestInt returns the value as a string, rather than an int), however the last one just returns "System.Collections.Generic.List`1[System.Int32]".

What am I supposed to do to pass a list to angular via $http.get? What if it's my own custom object? Would it be wise to still just use SignalR to get the data, and then use Angular to just call the SignalR methods?

2
  • 1
    Are you using WebApi? Commented Feb 25, 2015 at 14:27
  • Please provide the full code of your MVC controller as normally your are not directly returning the values. Instead you return ActionResults with the result embedded Commented Feb 25, 2015 at 14:29

3 Answers 3

2

If you're not using WebApi but just plain ASP.NET MVC you may have to do the following

public JsonResult TestList()
{
    return this.Json(new List<int> { 1, 2, 3 });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm. But if I used WebApi, could I return a List? I don't really want all my methods to just have JsonResult return types.
Yes, if you use WebApi then you can return a List<T>
Cool! I just changed things around to use WebApi, and that does exactly what I want! Thanks.
@RamblerToning Not at all. Happy coding.
0

in angularjs controller you have to call angular service in success event you can store the data to scope

    app.controller('mycontroler',['$scope','$http', function($scope,$http){

    $http.get('/UserRequest/TestInt').success(function (data) {


    $scope.data= data; //this scope object we can access in html
    });


}]) 

1 Comment

Yeah, I know that, I'm mostly just concerned about how to get the data from C# in the first place - and I'll worry about what to do with it when I actually receive it in the form I want.
0

That's because in ASP.NET MVC when you simple return a type that doens't inherit from ActionResult the framework "stringfies" it by executing the ToString() method.

You can either use a JsonResult (best option) or you can serialize yourself like this:

return Newtonsoft.Json.JsonConvert.SerializeObject(new List<int>() { 2, 3, 4, 5, 5 }) 

(returns a string)

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.