0

how to Deleting multiple Record with apiController and angular ?

I've tried the following. But I did not succeed

api controller :

 public class NewsCategoriesController : ApiController
    {
        private readonly IJN_NewsCategoriesService _ijnNewsCategoriesService;

        public NewsCategoriesController(IJN_NewsCategoriesService ijnNewsCategoriesService)
        {
            _ijnNewsCategoriesService = ijnNewsCategoriesService;
        }
        public void Delete(int id)
        {
           _ijnNewsCategoriesService.DeleteNewsCategory(id);
        }
        public void ManyDelete(DeleteViewModel ids)
        {
            var d = ids;
        }
    }
    public class DeleteViewModel
    {
        public int[] ids { get; set; }
    }
}

ajax methods :

deleteNewsCategories: function (id) {
                    return $http({
                        method: 'DELETE',
                        url: '/api/newsCategories/' + id
                    });
                },

deleteManyNewsCategories: function (ids) {
                    return $http({
                        method: 'DELETE',
                        url: '/api/newsCategories/' + JSON.stringify(ids),
                        traditional: true
                    });
                }

Delete a record works properly. To delete multiple record, the following error occurs:

    Failed to load resource: the server responded with a status of 400 (Bad Request)
 http://localhost:25768/api/newsCategories/[12,26]
4
  • Perhaps this is your solution: stackoverflow.com/a/9692974/2415971 Commented Aug 7, 2014 at 13:34
  • thanks. but The problem is not solved with this solution Commented Aug 7, 2014 at 13:39
  • Do other HTTP methods (GET, POST) work ok? Commented Aug 7, 2014 at 13:41
  • It seems to me that you're making the same call in both instances to a DELETE function that only accepts an integer. I'm not sure how ApiController operates (never used it myself) but it seems your ManyDelete is never reached. Commented Aug 7, 2014 at 14:30

1 Answer 1

0

The problem was solved.

 public void Delete(int id)
    {
       _ijnNewsCategoriesService.DeleteNewsCategory(id);
    }
    public void DeleteModels(DeleteViewModel dvm)
    {
        var d = dvm;
    }


      public class DeleteViewModel
        {
            public int[] Ids { get; set; }
        }

$http methods for delete:

             deleteNewsCategories: function (id) {
                return $http({
                    method: 'DELETE',
                    url: '/api/newsCategories/' + id
                });
            },

            deleteManyNewsCategories: function (ids) {
                return $http({
                    headers: {
                        'Content-type': 'application/json'
                    },
                    method: "DELETE",
                    url: "/api/newsCategories/",
                    data: { Ids: ids }
                });
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.