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權所希望的方式:
這裏是.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; }
}
在這裏我可以得到我的頂級屬性:
現在,我嘗試從到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對象是後空。 我甚至沒有獲得頂級屬性。
有沒有辦法來發布這樣的複雜對象?