如何在角度js中传递数组列表?

时间:2016-06-23 12:27:37

标签: angularjs api

我使用restangular post方法将我的JSON发送到Web API。 在控制台窗口,我可以看到$ scope.school正在填充详细信息,但在web API我没有得到' fileSet'对象值。

我的角度代码是: -

      $scope.school = {
            'class': '94E38941-5A97-4A9B-B82A-8D53C9DBEE96',
            'inputInstanceId': 'CBBCD28D-F144-45E2-A494-C767549BD3AF',
            'inboxType': '8',
            'schoolTitle': $scope.value1,
            'schoolDescription': $scope.value2,
            'schoolContext': $scope.ContextListL1["Value"],
            'schoolGuid ': $cookieStore.get('objschoolid'),
            'fileSet': {
                'fName': fileName,
                'fileUploadId': fileId
            }
        }

其中我的api调用代码是angular

 Restangular.one(routeString).post('', $scope.school, params, headers).then(function (resp)

我的API类是

public class CreateClassDTO
{
    public Guid class { get; set; }
    public Guid inputInstanceId { get; set; }
    public int inboxType { get; set; }
    public string schoolTitle { get; set; }
    public string schoolDescription { get; set; }
    public string schoolContext { get; set; }
    public Guid schoolGuid { get; set; }
    public List<FileuploadResult> fileSet { get; set; }

}

public class FileuploadResult
{
    public string fName { get; set; }
    public Guid fileUploadId { get; set; }
}

3 个答案:

答案 0 :(得分:2)

我将假设fileSet应该是一个对象数组而不是一个对象:

'fileSet': [
    {
        'fName': fileName,
        'fileUploadId': fileId
    }
]

答案 1 :(得分:0)

您正在引用文件集中的两个变量

'fileSet': {
    'fName': fileName,
    'fileUploadId': fileId
}

即fileName和fileId。是否定义了这些变量?

答案 2 :(得分:0)

您可以在Object中执行Do对象。像这个例子

在Mvc

public class Userplusresponse
{
    public User user { get; set; }
    public string response { get; set; }
}



public class User
    { 
    public string username{get; set;}
    public string firstName { get; set; }
    public string LastName { get; set; }
    public string password { get; set; }
    public string con_password { get; set; }    
    public int phoneNumber { get; set; }
    }

Angular或Ajax Post

 $scope.User = {}; //**Add more values
 var dataplusresponse = {}
                dataplusresponse.user = User;
                dataplusresponse.response = response;

 $http({
                    method: 'POST',
                    url: '@Url.Action("Register", "Account")',
                    data: dataplusresponse, //forms user object
                    headers: { 'Content-Type': "application/json" }
                })
          .success(function (data) {

              console.log(data);
}

和控制器

[HttpPost]
        public ActionResult Register(Userplusresponse UR)
        {
            var captchaResponse = UR.response;  //this is object of response

            var UserData = UR.user;  //this is object of $scope.user
//You can get child object from UserData