2

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 frombody attribute to retreive data like this -public string GetIssueDescription([FromBody]dynamic issueId).But I want that if I could do like this public string GetIssueDescription(int issueId)

angularjs controller code

 //show issue details $scope.showIssueDetails = function (issue) { //$scope.issueCount = 2; $scope.issueDetailsLoaded = false; $scope.selectedIssue = issue; $scope.statusName = issue.StatusName; var issueId = issue.Id; var url = window.location.protocol + '//' + window.location.host + '/api/Issues' + '/GetIssueDescription/'; $http.post(url, JSON.stringify({ issueId: issueId })).success(function (data, status, headers, config) { if (data != '' || data.length >= 0) { $scope.selectedIssue.Description = $sce.trustAsHtml(angular.fromJson(data)); $scope.selectedIssue = issue; $scope.showedit = true; $scope.showeditdesc = true; //setting default properties //$scope.issue.DueDate = $scope.selectedIssue.DueDate; $scope.getIssueComment($scope.selectedIssue); $scope.issueDetailsLoaded = true; } else if (data == '') { $scope.selectedIssue.Description = ""; } else { $scope.errors.push(data.error); } }); if($scope.isVisible==false) { $("#changedetailsbox").hide(); $scope.isVisible = true; } if ($scope.isVisibleReply == false) { $("#postReplybox").hide(); $scope.isVisibleReply = true; } }; 

MVC controller code

 [HttpPost] [AuthenticationRequired] public string GetIssueDescription([FromBody]dynamic issueId) { try { var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(issueId.ToString()); var selectedIssueId = new Guid(dictionary["issueId"]); var result = PublicLayer.GetIssueDescription(selectedIssueId); return result.IsSuccessful ? result.Result : string.Empty; } catch (Exception ex) { BLL.Base.BaseLayer.WriteApplicationLog(ex); return string.Empty; } } 

Please suggest how I could directly use my json data in my mvc controller.I took reference of this post but it didn't worked for me-Code I referred

I already have an issue model

 public partial class Issue { public Guid Id { get; set; } public Guid ProjectId { get; set; } public Guid CreatorUserId { get; set; } public Guid AssignedUserId { get; set; } public Guid OwnerUserId { get; set; } public Guid LastUpdatedUserId { get; set; } public int DisplayId { get; set; } public string Title { get; set; } public string Description { get; set; } public int IssueStatusId { get; set; } public int IssuePriorityId { get; set; } public int IssueTypeId { get; set; } public int IssueCategoryId { get; set; } public Guid? AffectedMilestoneId { get; set; } public int IssueResolutionId { get; set; } public DateTime DueDate { get; set; } public Guid? MilestoneId { get; set; } public decimal Estimation { get; set; } public int Progress { get; set; } public string SenderEmailAddress { get; set; } public bool IsDeleted { get; set; } public DateTime CreatedOn { get; set; } public DateTime LastUpdatedOn { get; set; } } 

Please suggest is it possible to use my this class in my method-public string GetIssueDescription(Issue issue) like this and can retreive my id by using issue.Id.Please suggest how to achieve this.

5
  • 1- you do not need to stringify . 2 - Create a model for your Issue that has a property of int issueID , then change your GetIssueDescription(int issueID) to GetIssueDescription( Issue issue )
    – G-Man
    CommentedJun 2, 2015 at 12:54
  • Are you using API Controller (As per your URL) or only MVC Controller ?? First check your URL. and also no need to use JSON.stringify in http post in angularjs. You can use code like $http.post('url?issueId='+issueId , ).success(function (data, status, headers, config) {//do whatever have to do with data}. If you are not using API Controller and have'n't defined custom ROUTE then your URL is wrong.CommentedJun 2, 2015 at 12:56
  • yeah I am using api controllerCommentedJun 2, 2015 at 12:59
  • stackoverflow.com/users/130211/gaetano please see my updated code and suggest how to use it to retreive my int idCommentedJun 2, 2015 at 13:07
  • take a look at my answer below
    – G-Man
    CommentedJun 2, 2015 at 13:13

1 Answer 1

3

In your controller

change

public string GetIssueDescription([FromBody]dynamic issueId) 

to

public string GetIssueDescription(Issue issue) 

in your angular POST

var issueId = issue.Id; . . $http.post(url, { Id: issueId }).success(function (data, status, headers, config) { 
0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.