2017-05-03 40 views
0

我想从一个Angular post发布一个对象到一个ASP.Net MVC Core Web API Post方法。我可以使用属性将复杂对象发送给Core Web API中的FromBody参数吗?

的角度功能是这样的:

 $scope.addAction = function (pAction, pCaseId) { 
      pAction.caseId = pCaseId; 

      $http.post(actionUrl, pAction) 
       .then(function (response) { 
       $scope.data.actions.push(response.data); 
       }) 
       .catch(function (error) { 
       $scope.data.ActionInsertError = error; 
       }); 
     } 

一切都得到建正是我的前POST权所希望的方式:

enter image description here

这里是.NET核心Web API POST :

[HttpPost] 
public async Task<Generic.Model.Lerd.Action> Post([FromBody]ActionFromBodyModel model) 
{ 
    long result; 
    Generic.Model.Lerd.Action action; 
    using (var conn = _context.Database.GetDbConnection()) 
    { 
     conn.Open(); 

     using (var command = conn.CreateCommand()) 
     { 
      StringBuilder sb = new StringBuilder(); 
      sb.Append("INSERT INTO Actions "); 
      sb.Append("(ActionStatus, ActionTypeId, CaseId, DateCreated, Notes) "); 
      sb.Append("VALUES "); 
      sb.Append("(@ActionStatus, @ActionTypeId, @CaseId, @DateCreated, @Notes); "); 
      sb.Append("SELECT CAST(scope_identity() AS int);"); 

      command.CommandText = sb.ToString(); 

      command.Parameters.Add(new SqlParameter("@ActionStatus", SqlDbType.Int) { Value = model.ActionStatus }); 
      command.Parameters.Add(new SqlParameter("@ActionTypeId", SqlDbType.BigInt) { Value = model.ActionTypeId }); 
      command.Parameters.Add(new SqlParameter("@CaseId", SqlDbType.BigInt) { Value = model.CaseId }); 
      command.Parameters.Add(new SqlParameter("@DateCreated", SqlDbType.DateTime) { Value = DateTime.Now }); 
      command.Parameters.Add(new SqlParameter("@Notes", SqlDbType.NVarChar) { Value = model.Notes }); 

      try 
      { 
       result = (int)command.ExecuteScalar(); 
       action = await _genericService.GetSingleIncludingAsync(result, 
        a => a.ActionType); 
      } 
      catch(Exception ex) 
      { 
       throw ex; 
      } 
     } 

    } 

    return action; 
} 

这里是FromBody模型:

public class ActionFromBodyModel 
{ 
    public long CaseId { get; set; } 
    public long ActionTypeId { get; set; } 
    public long ActionStatus { get; set; } 
    public string Notes { get; set; } 
} 

在这里我可以得到我的顶级属性:

enter image description here

现在,我尝试从到pCASE的ActionType1对象的角度支柱到Web API发布。

所以我补充一点,属性到FromBody模式:

public class ActionFromBodyModel 
{ 
    public long CaseId { get; set; } 
    public long ActionTypeId { get; set; } 
    public long ActionStatus { get; set; } 
    public string Notes { get; set; } 

    public ActionType1 ActionType1 { get; set; } 
} 

ActionType1看起来是这样的:

public class ActionType1 : BaseEntity 
{ 
    [ForeignKey("Id")] 
    public long ActionId { get; set; } 
    public virtual Action Action { get; set; } 

    public long ActionProposedBySupervisorId { get; set; } 
    [ForeignKey("ActionProposedBySupervisorId")] 
    public LookupDetail ActionProposedBySupervisor { get; set; } 

    public long ActionTakenBySupervisorId { get; set; } 
    [ForeignKey("ActionTakenBySupervisorId")] 
    public LookupDetail ActionTakenBySupervisor { get; set; } 

    public DateTime ActionEffectiveDate { get; set; } 
} 

但是,当我加入这个属性,我的整个FromBody对象是后空。 我甚至没有获得顶级属性。

有没有办法来发布这样的复杂对象?

回答

0

事实证明,我的命名从Angular模型转变为后端模型。

我刚刚在Id中加入了Angular模型中的大多数属性,以匹配表单中我的ng模型属性上的后端模型。

相关问题