Получить массив объектов asp net API - PullRequest
1 голос
/ 11 марта 2020

У меня есть приложение angular, которое отправляет массив объектов и сервер API. NET.

Проблема в том, что my. NET получает данные, но не может их привязать к объекты правильно.

Angular Приложение внешнего интерфейса:

data: IAccountBookKeeping[] = [];

onClickSendToAPI(){

const httpOptions = {
  headers: new HttpHeaders({'Content-Type': 'application/json'})
}

 const stringify = fastJson({
  type: 'object',
  properties: {
    AccountingDate: {
      type: 'string'
    },
    RegistrationNo: {
      type: 'string'
    },
    Currency: {
      type: 'string'
    },
    IDKT: {
      type: 'string'
    },
    OriginalIDKT: {
      type: 'string'
    },
    CounterAccountIDKT: {
      type: 'string'
    },
    ProjectCode: {
      type: 'string'
    },
    Balance: {
      type: 'string'
    },
    Text: {
      type: 'string'
    }
  }
})


const jsonString: string[] = [];

this.data.forEach(element => {
  jsonString.push(stringify(this.data[0]));
});

this.http.post('http://localhost:5000/api/GFSAccount',
JSON.stringify(jsonString),
 httpOptions).subscribe(reponse =>
console.log(reponse));

}

Что Angular Передача интерфейса в API

["{\ "AccountingDate \": \ "20171130 \", \ "RegistrationNo \": \ "39A1 \", \ "Валютная \": \ "DKK \", \ "IDKT \": \ "34HS991016 \", \ "OriginalIDKT \ ": \" тест \», \ "CounterAccountIDKT \": \ "34HS980876 \", \ "ProjectCode \": \ "078 \", \ "Баланс \": \ "1 \", \ "Текст \" : \ "006-Hest prov-Ytd NOV17 \"} "," {\ "AccountingDate \": \ "20171130 \", \ "RegistrationNo \": \ "39A1 \", \ "Валюта \": \ "DKK \», \ "IDKT \": \ "34HS991016 \", \ "OriginalIDKT \": \ "тест \", \ "CounterAccountIDKT \": \ "34HS980876 \", \ "ProjectCode \": \ "078 \" , \ "Balance \": \ "1 \", \ "Text \": \ "006-Hest prov-Ytd NOV17 \"} "

ASP. NET Сервер получения API

    [HttpPost]
    public IActionResult Post([FromBody] IEnumerable<AccountBookKeeping> request)
    {
      return Ok(request);
    }

ASP. NET AccountBookKeeping Class

public class AccountBookKeeping
{
    public string AccountingDate { get; set; }
    public string RegistrationNo { get; set; }
    public string IDKT { get; set; }
    public string OriginalIDKT { get; set; }
    public string CounterAccountIDKT { get; set; }
    public string Text { get; set; }
    public string ProjectCode { get; set; }
    public string Currency { get; set; }
    public string Balance { get; set; }
}

1 Ответ

1 голос
/ 11 марта 2020

Если вы хотите разместить массив как тело post, просто передайте массив напрямую. Нет необходимости интенсивно обрабатывать его через JSON.stringify.

const url = 'http://localhost:5000/api/GFSAccount';
this.http.post(url, this.data, httpOptions).subscribe(reponse =>
  console.log(reponse);
});

Кроме того, существующий l oop просто строит массив с повторением первого элемента:

this.data.forEach(element => {
  jsonString.push(stringify(this.data[0]));
});

Но этот шаг не имеет значения, если вы просто передаете массив как тело.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...