angularjs以json格式将数据发布到mvc控制器

时间:2015-06-02 12:48:41

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

我正在开发一个项目,我在其中使用了angularjs和mvc。我正在通过$ http.post()将数据从角度js控制器传递到我的mvc控制器。现在我使用frombody属性来检索这样的数据-public string GetIssueDescription([FromBody] dynamic issueId)。但是我想要,如果我能这样做的话 public string GetIssueDescription(int issueId)

angularjs控制器代码

 //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控制器代码

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

请建议我如何直接在我的mvc控制器中使用我的json数据。我参考了这篇文章,但它对我不起作用 - Code I referred

我已经有了问题模型

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

请建议是否可以在我的方法 - 公共字符串GetIssueDescription(问题问题)中使用我的这个类,并使用issue.Id修改我的ID。请建议如何实现此目的。

1 个答案:

答案 0 :(得分:3)

在您的控制器中

更改

public string GetIssueDescription([FromBody]dynamic issueId)

public string GetIssueDescription(Issue issue)
在你的角度POST中

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