0

I am working on a project in which I have used angularjs and mvc.I am passing data from angular js controller to my mvc controller by $http.post().For now I am using single object/json array to retreive data like this -public bool UpdateIssueDetails(IssueBO issue).But I want that if I could do like this public public bool UpdateIssueDetails(IssueBO issue,List lstMembersToNotify).I want to send two json arrays from ny angular js controller to my above mvc controller method.

angularjs controller code

 $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 controller code

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

I am passing two json array from my angularjs controller like this-$http.post(url, [issueDetails, arrMembersToNotifyNew]).success(function (data, status, headers, config).But I am getting error trying this.Please suggest how to achieve this.Thanks

2
  • What the error says?
    – adricadar
    CommentedJun 3, 2015 at 7:07
  • Failed to load resource: the server responded with a status of 500 (Internal Server Error)CommentedJun 3, 2015 at 7:18

2 Answers 2

1

You need to pass data to the action by using JSON.stringify()

$http.post(url, JSON.stringify({ issue: issueDetails, lstMembersToNotify: arrMembersToNotifyNew }); 
2
  • If he stringify's the object, won't he have to deserialize it? In which case, wouldn't sending it as an object be enough?CommentedJun 3, 2015 at 7:17
  • @SylvanDAsh we need to [FromBody] annotation in front of action parameter of controller...that would not lead to do stringify()asp.net/web-api/overview/formats-and-model-binding/…CommentedJun 3, 2015 at 7:20
0

Post it as properties of an object.

$http.post(url, { issue: issueDetails, lstMembersToNotify: arrMembersToNotifyNew }); 
2
  • still not working getting this error-Failed to load resource: the server responded with a status of 500 (Internal Server Error)CommentedJun 3, 2015 at 7:19
  • Debug the asp.net controller method. 500 usually means an exception was thrown on the server.
    – ChrisV
    CommentedJun 3, 2015 at 7:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.