новый в угловой и asp.net ядре, спасибо за вашу помощь.
модели: quotation.ts vehicle.ts car.ts
export interface Quotation {
id: number;
name: string;
cars: Car[];
}
export interface Vehicle {
id: number;
brand: string;
}
export interface Car extends Vehicle {
weight: number;
}
Служба цитат
export class QuotationService {
baseUrl = environment.apiUrl;
constructor(private http: HttpClient) { }
addQuotation(model: any) {
return this.http.post(this.baseUrl + 'quotation', model);
}
//getCars(): Observable<Car[]> {
// return this.http.get<Car[]>(this.baseUrl + 'quotation');
//}
Компонент quotation.ts
export class QuotationFormComponent implements OnInit {
model: any = {};
constructor(private quotationService: QuotationService) {}
ngOnInit() {
//this.model.cars = this.quotationService.getCars();
}
saveQuotation() {
this.quotationService.addQuotation(this.model).subscribe(() => {
console.log('success');
}, error => {
console.log(error);
});
}
Цитата компонента html
<form #quotationForm="ngForm" (ngSubmit)="saveQuotation()">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="name" name="name" [(ngModel)]="model.name">
</div>
<div class="form-group">
<input type="text" class="form-control" id="weight" placeholder="Weight" name="weight" [(ngModel)]="model.cars.weight">
</div>
<div class="form-group">
<button class="btn btn-success" type="submit">Valider</button>
</div>
</form>
Первая ошибка:
ОШИБКА TypeError: Невозможно прочитать свойство 'weight'неопределенного в Object.eval [как updateDirectives] (QuotationFormComponent.html: 40)
Исправлено с помощью: initialization of model.cars = this.quotationService.getCars () Но я не понимаю, почему янужно загрузить всю мою базу данных автомобилей для регистрационной формы?
Ошибка2 после исправления 1:
HttpErrorResponse {заголовки: HttpHeaders, статус: 400, statusText: «Bad Request», URL: "http://localhost:xxxx/api/quotation", ok: false,…} ошибка:" Невозможно десериализовать текущий объект JSON (например, {"name": "value"}) в тип 'System.Collections.Generic.List`1 [BLABLA.API.Models.Car] ', поскольку для корректной десериализации типу требуется массив JSON (например, [1,2,3]).Чтобы исправить эту ошибку, либо измените JSON на массив JSON (например, [1,2,3]), либо измените десериализованный тип так, чтобы это был нормальный тип .NET (например, не примитивный тип, например, целое число, а не тип коллекции, например,массив или список), которые можно десериализовать из объекта JSON.JsonObjectAttribute также можно добавить к типу, чтобы заставить его десериализоваться из объекта JSON.Путь 'cars._isScalar', строка 1, позиция 21. "
Чего мне не хватает? Как создать массив ввода ngModel для объектов List?
Спасибо, извините затекстовые пояснения.