简体   繁体   English

如何将列表从C#angular传递到Angular控制器?

[英]How do I pass a List from C# angular to Angular controller?

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#. 我是Angular的新手,我想在.NET MVC应用程序中实现它,但是我对如何从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? 在我以前的应用程序中,我已经使用SignalR进行所有的客户端-服务器通信,并且可以将对象从服务器无缝地返回到客户端,而无需进行字符串化,序列化或反序列化。.我想使用Angular并不是那么容易吗? 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 然后在我的Angular控制器中,我有一个使用$ http.get调用这些方法的函数

 $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]". 现在,前两个工作按预期进行(尽管TestInt以字符串形式而不是int返回值),但是最后一个仅返回“ System.Collections.Generic.List`1 [System.Int32]”。

What am I supposed to do to pass a list to angular via $http.get? 我应该怎么做才能通过$ http.get将列表传递给angular? 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? 仍然仅使用SignalR来获取数据,然后使用Angular来仅调用SignalR方法是否明智?

If you're not using WebApi but just plain ASP.NET MVC you may have to do the following 如果您不使用WebApi,而仅使用普通的ASP.NET MVC,则可能必须执行以下操作

public JsonResult TestList()
{
    return this.Json(new List<int> { 1, 2, 3 });
}

in angularjs controller you have to call angular service in success event you can store the data to scope 在angularjs控制器中,必须在成功事件中调用angular服务,然后才能将数据存储到范围

    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
    });


}]) 

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. 那是因为在ASP.NET MVC中,当您简单地返回一个不会从ActionResult继承的类型时,框架通过执行ToString()方法来“字符串化”它。

You can either use a JsonResult (best option) or you can serialize yourself like this: 您可以使用JsonResult(最佳选项),也可以像这样序列化自己:

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

(returns a string) (返回一个字符串)

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

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