2021年3月29日星期一

The JSON value could not be converted to System.Boolean. Path: $

I'm trying to sent request from Angular front to asp.net API server. The form to be submitted is defined as:

export class SaleToUpdate {      id: number;      shipped: boolean;      shipDate: string;      returned: boolean;      returnDate: string;  }

the related template lines in mat-table as:

<mat-grid-tile [colspan] = "2" [rowspan] = "1">                  <mat-form-field>                      <input formControlName="shipDate" matInput placeholder="Ship Date">                                                           </mat-form-field>                                  <div class="radio-group ml-3">                      <mat-radio-group formControlName="shipped" required (ngModelChange)="shipped">                                     <mat-radio-button value="false">No</mat-radio-button>                          <mat-radio-button value="true" class="ml-4">Yes</mat-radio-button>                      </mat-radio-group>                  </div>  </mat-grid-tile>  

The onSubmit() in component:

onSubmit() {          if (this.salesService.saleForm.valid) {        let sale: SaleToUpdate = this.salesService.saleForm.value;          console.log(sale);           this.salesService.updateSale(sale.id, sale).subscribe(res => {          console.log(res);        }, error => {          console.log(error);        });      }     }

Therefore, basically, my purpose it to send the from data contain two bool and two date values to server.

On backside, I used a form to receive the above request (API test is fine)

using System;    namespace API.Dtos  {      public class SaleUpdateDto      {                public int Id { get; set; }                    public bool Shipped { get; set; }          public DateTime ShipDate { get; set; }                 public bool Returned { get; set; }          public DateTime ReturnDate { get; set; }      }  }

From the console, I got the error "The JSON value could not be converted to System.Boolean. Path: $.shipped | LineNumber: 0 | BytePositionInLine: 274." when click onSubmit from the client.

I noticed the values from the form are string with quotation marks other than bool values true or false without quotations, something like: shipped: "true" returned: "true"

Can anyone please help me out with any possible solutions?

https://stackoverflow.com/questions/66864084/the-json-value-could-not-be-converted-to-system-boolean-path March 30, 2021 at 10:41AM

没有评论:

发表评论