angularjs将数据发布到具有多个数组的json格式的mvc控制器

时间:2015-06-03 07:05:09

标签: javascript json asp.net-mvc angularjs asp.net-mvc-4

我正在开发一个项目,我在其中使用了angularjs和mvc。我正在通过$ http.post()将数据从角度js控制器传递到我的mvc控制器。现在我使用单个对象/ json数组进行检索像这样的数据-public bool UpdateIssueDetails(IssueBO问题)。但是我希望如果我可以做这样的公共公共bool UpdateIssueDetails(IssueBO问题,List lstMembersToNotify)。我想从ny angular js控制器发送两个json数组到我上面的mvc控制器方法。

angularjs控制器代码

 $scope.saveIssueDetails = function (issue) {
    var milestoneId = "";
    var milestoneName = "";
    if ($scope.selectedIssue.Milestone== undefined) {
        milestoneId = "";
        milestoneName = "";
    } else {
        milestoneId = $scope.selectedIssue.Milestone.Id;
        milestoneName = $scope.selectedIssue.Milestone.Name;
    }
    var arrMembersToNotify = [];
    var arrMembersToNotifyNew = [];
    var iCount = 0;
    $("#membersToNotify input[type=checkbox]:checked").each(function () {
        arrMembersToNotify = $(this).val().split("~");
        arrMembersToNotifyNew.push({ "UserId": arrMembersToNotify[0], "UserDisplayName": arrMembersToNotify[1], "Email": arrMembersToNotify[2] });         
    }); 
    var issueDetails =
   {
    Id: issue.Id,
    ProjectId: issue.ProjectId,
    ProjectName: issue.ProjectName,
    IssueStatusId: $scope.selectedIssue.Status.Id,
    StatusName: $scope.selectedIssue.Status.Name,
    IssuePriorityId: $scope.selectedIssue.Priority.Id,
    PriorityName: $scope.selectedIssue.Priority.Name,
    AssignedUserId: $scope.selectedIssue.AssignedTo.Id,
    AssigneeDisplayName: $scope.selectedIssue.AssignedTo.DisplayName,
    IssueCategoryId: $scope.selectedIssue.Category.Id,
    CategoryName: $scope.selectedIssue.Category.Name,
    DueDate: $scope.selectedIssue.DueDate,
    OwnerUserId: $scope.selectedIssue.OwnedBy.Id,
    OwnerDisplayName: $scope.selectedIssue.OwnedBy.DisplayName,
    IssueTypeId: $scope.selectedIssue.Type.Id,
    IssueTypeName: $scope.selectedIssue.Type.Name,
    IssueResolutionId: $scope.selectedIssue.Resolution.Id,
    ResolutionName: $scope.selectedIssue.Resolution.Name,
    MilestoneId: milestoneId,
    MilestoneName: milestoneName,
    Estimation: $scope.selectedIssue.Estimation,
    Progress: $scope.selectedIssue.Progress,
   };
    var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/UpdateIssueDetails/';
    $http.post(url, [issueDetails, arrMembersToNotifyNew]).success(function (data, status, headers, config) {
        if (data != '' || data.length >= 0 || data == true) {
            //$scope.selectedIssue = issue;
            //$scope.showIssueDetails($scope.selectedIssue);
            $scope.GetAssignedIssues();
        }
        else if (data == '' || data == false) {
            $scope.selectedIssue = null;
        } else {
            $scope.errors.push(data.error);
        }
    });
};

mvc控制器代码

  [HttpPost]
    [AuthenticationRequired]
    public bool UpdateIssueDetails(IssueBO issue,List<IssueNotification> lstMembersToNotify)
    {
        try
        {
            //var issueDetails = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(issueAllDetails[0].ToString());
            //List<Dictionary<string, string>> membersToNotifyDetails = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(issueAllDetails[1].ToString());
            var membersToNotify = lstMembersToNotify.Select(membersToNotifyDetail =>
                                                                new IssueNotification()
                                                                {
                                                                    UserId =membersToNotifyDetail.UserId,
                                                                    Email =
                                                                        membersToNotifyDetail.Email,
                                                                    UserDisplayName =
                                                                        membersToNotifyDetail.UserDisplayName
                                                                }).ToList();
            var newIssue = new IssueBO
            {
                OwnerUserId = issue.OwnerUserId,
                OwnerDisplayName = issue.OwnerDisplayName,
                LastUpdatedUserId = SessionItems.UserId,
                LastUpdaterDisplayName = SessionItems.DisplayName,
                LastUpdatedOn = DateTime.Now,
                ProjectId = issue.ProjectId,
                ProjectName = issue.ProjectName,
                Id = issue.Id,
                AssignedUserId = issue.AssignedUserId,
                AssigneeDisplayName = issue.AssigneeDisplayName,
                IssueStatusId = issue.IssueStatusId,
                StatusName = issue.StatusName,
                Progress = issue.Progress,
                IssuePriorityId = issue.IssuePriorityId,
                PriorityName = issue.PriorityName,
                IssueTypeId = issue.IssueTypeId,
                IssueTypeName = issue.IssueTypeName,
                IssueCategoryId = issue.IssueCategoryId,
                CategoryName = issue.CategoryName,
                IssueResolutionId = issue.IssueResolutionId,
                ResolutionName = issue.ResolutionName,
                DueDate = issue.DueDate,
                Estimation = issue.Estimation,
                MilestoneId = issue.MilestoneId,
                MilestoneName = issue.MilestoneName
            };
            var result = BLL.AdminLayer.UpdateIssueDetail(newIssue, membersToNotify);
            return result.IsSuccessful && result.Result;
        }
        catch (Exception ex)
        {
            BLL.Base.BaseLayer.WriteApplicationLog(ex);
            return false;
        }
    }

我从我的angularjs控制器传递两个json数组,如this- $ http.post(url,[issueDetails,arrMembersToNotifyNew])。success(function(data,status,headers,config)。但是我在尝试这个时遇到错误。请建议如何实现这一点。谢谢

2 个答案:

答案 0 :(得分:1)

您需要使用JSON.stringify()

将数据传递给操作
$http.post(url, JSON.stringify({ issue: issueDetails, 
     lstMembersToNotify: arrMembersToNotifyNew 
});

答案 1 :(得分:0)

将其作为对象的属性发布。

$http.post(url, { issue: issueDetails, lstMembersToNotify: arrMembersToNotifyNew });