4

how do I post an array to an action on my controler with the anti forgery token.

This is my Jquery postdata:

var postData = { '__RequestVerificationToken': $('input[name=__RequestVerificationToken]').val(), 'productIds': IDs };

this is my Jquery post:

$.post("MyProducts/DeleteProduct" , postData, function(data) { });

This is my action:

public void DeleteProduct(List<int> productIds)
    {
        foreach (int i in productIds)
        {
            _repository.DeleteProduct(i, null);
        }        
    }

I also use an object to store my anti forgery token and I wonder how I can use that with the postdata.

This is the token object:

var token = { '__RequestVerificationToken': $('input[name=__RequestVerificationToken]').val() };

Kind regards

Pickels
  • 33,902
  • 26
  • 118
  • 178
  • might be useful... http://stackoverflow.com/questions/4074199/jquery-ajax-calls-and-the-html-antiforgerytoken/4074289 – JeremyWeir May 29 '12 at 18:00

2 Answers2

3
var ids = [1,2];

var data = {
__RequestVerificationToken : token,
productIds : ids
};

$.post(url, data, function() ...

where token is the var you mentioned

Dave Archer
  • 3,022
  • 20
  • 23
1

Assuming you have all your product IDs in the HTML it would be much easier to use jqueryForm plugin:

$("form").ajaxSubmit({url: "MyProducts/DeleteProduct", success: function(response) {
  // Handle the response
}})
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
  • That would mean I have to add a second form to my view and a second AntiForgeryToken I guess. Also the IDs are extracted from a table row. The IDs are like this: id="productRow-1", id="productRow-2, ... – Pickels Aug 07 '09 at 05:12