skip to Main Content

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

2

Answers


  1. 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")]

    Login or Signup to reply.
  2. 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 } });
      }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search