Home > Software design >  Not able to delete multiple records from database having array of id's
Not able to delete multiple records from database having array of id's

Time:06-10

I have a service.ts file that calls HTTP Client method to delete multiple records from db. But when I'm passing multiple Id's in an array it's not getting deleted and an error shows up in the network.

Controller.cs

[HttpDelete]
        public void DeleteMulipleDevice([FromQuery]int[] deviceIds)
        {
            try
            {
                _frontLineContext.Devices.RemoveRange(_frontLineContext.Devices.Where(d => deviceIds.Contains(d.Id)));
                _frontLineContext.SaveChanges();
            }
            catch (Exception)
            {
            }
        } 

service.ts

public deleteMulipleDevices(deviceIds: any): Observable<any> {
    let params = new HttpParams().set("deviceIds", deviceIds);
    return this.http.delete<any[]>(this.baseUrl   'device', { params });
  }

deviceIds = [1,2,3,4] looks like this.

What wrong should I add to the service.ts or Controller file to delete multiple records from the table?

Error enter image description here

CodePudding user response:

change the service.ts so that the deviceIds parameter look like this deviceIds=19&deviceIds=20 instead of deviceIds=19,20 then in your api method change [FromQuery] to [FromQuery(Name="deviceIds")]

CodePudding user response:

I just had to do replace my service.ts with this code

public deleteMulipleDevices(deviceIds: any): Observable<any> {
    return this.http.delete<any[]>(this.baseUrl   'device', { params: { deviceIds: deviceIds } });
  }

  • Related