Asp.net Mvc: jquery post token + anti-fake

how can I place an array for action on my controller with an anti-fake token.

This is my jquery post data:

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-fake token, and I wonder how I can use it with postdata.

This is a token object:

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

Yours faithfully

+4
source share
2 answers
 var ids = [1,2]; var data = { __RequestVerificationToken : token, productIds : ids }; $.post(url, data, function() ... 

where the token is the var you mentioned

+3
source

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

 $("form").ajaxSubmit({url: "MyProducts/DeleteProduct", success: function(response) { // Handle the response }}) 
+1
source

All Articles