views:

716

answers:

2

Hello,
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

+1  A: 

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
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
+1  A: 
var ids = [1,2];

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

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

where token is the var you mentioned

David Archer