I have implemented a dropdown that is populating correctly in my angular application but when i try to submit my form , it is showing the display text in the model instead of the Id. What could be the problem To my knowledge all i need is
[(ngModel)]="newJob.customerId
UI
<label for="customer">Customer</label>
<select name="customer" required #customer="ngModel" ngModel placeholder="Please select" [(ngModel)]="newJob.customerId">
<option [ngValue]="undefined" selected>Please select</option>
<option *ngFor="let customer of customers" >{{customer.name}} </option>
</select>
<small *ngIf="customer.invalid">Please select a customer</small>
Component
public createJob(form: NgForm): void {
if (form.invalid) {
alert('form is not valid');
} else {
console.log(this.newJob);
this.jobService.CreateJob(this.newJob).then(() => {
this.jobService.GetJobs().subscribe(jobs => this.jobs = jobs);
});
}
}
Model
export interface JobModel {
jobId: number;
engineer: string;
customerId: number;
customerName: string;
when: Date;
}
Update
Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":400,"statusText":"Bad Request","url":"http://localhost:63235/job","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://localhost:63235/job: 400 Bad Request","error":{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|9e7e208d-44c84ff36cde4107.","errors":{"$.customerId":["The JSON value could not be converted to System.Int32. Path: $.customerId | LineNumber: 0 | BytePositionInLine: 50."]}}}