user19132099
user19132099

Reputation:

Not able to delete multiple records from database having array of id's

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

Upvotes: 0

Views: 145

Answers (2)

user19132099
user19132099

Reputation:

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 } });
  }

Upvotes: 0

hphp
hphp

Reputation: 2272

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

Upvotes: 1

Related Questions