简体   繁体   中英

angularjs post data to mvc controller in json format

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.

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) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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