[英]angularjs post data to mvc controller in json format with multiple arrays
[英]angularjs post data to mvc controller in json format
我正在一个使用angularjs和mvc的项目中工作,正在通过$ http.post()将数据从angular js控制器传递到我的mvc控制器。 GetIssueDescription([FromBody] dynamic issueId)。但是,如果我可以像这样使用公共字符串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数据。我参考了这篇文章,但对我没有用- 我引用的代码
我已经有一个问题模型
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 issue)中使用我的此类,并且可以通过使用issue.Id检索我的ID。请建议如何实现。
在您的控制器中
更改
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) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.