asp.net mvc 5.0 中的 ValidateAntiForgeryToken - 如何使用 JSON 和 ajax 传递对象数组

ValidateAntiForgeryToken in asp.net mvc 5.0 - how to pass array of objects with JSON and ajax

似乎 ValidateAntiForgeryToken 属性阻止了数据在传递给 MVC 控制器时被正确解析,当我删除 ValidateAntiForgeryToken 属性但不使用它时,下面的代码有效,所有传递控制器操作中的参数,但翻译数组除外。

请指教如何在利用 ValidateAntiForgeryToken 属性的同时传递对象数组,这可能吗?

这是我的代码

C#

 [HttpPost]
 [ValidateAntiForgeryToken]
 public void AddComment( string code, string type, string ecomment, IEnumerable<CommentTranslation> translations) 
  {
            //do something later
   }

评论翻译为

public class CommentTranslation
    {
        public string LangId { get; set; }
        public string LangName { get; set; }
        public string Translation { get; set; }
    }

js

addComment: function (ecomment, type, translations) {


        var data = {           
                code: '',
                type: type,
                ecomment: ecomment,
                translations: translations
        };

        var url = 'CommentsAjax/AddComment';
        return comments.repository.postDataWithToken(data, url);
    },

  postDataWithToken: function (data, url) {
        
        return $.ajax({
            type: 'POST',
            traditional: true,
            contentType: 'application/x-www-form-urlencoded; charset=utf-8',
            data: comments.repository.addAntiForgeryToken(data),
            url: getServerPath() + url
        });
    }


addAntiForgeryToken: function (data) {
    var token = $('input[name="__RequestVerificationToken"]').val();
    data.__RequestVerificationToken = token;
    return data;
},

最终使用了 FormCollection,您可以将任何内容传递给控制器​​。