简体   繁体   中英

Asp.net Mvc: Jquery post array + anti forgery token

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

var ids = [1,2];

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

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

where token is the var you mentioned

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM