简体   繁体   English

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

It seems that ValidateAntiForgeryToken attribute prevents the data from being parsed properly when passed to MVC Controller, the code below works when I remove the ValidateAntiForgeryToken attribute but does not work with it, all the parameters in the controller action are passed except array of translations.似乎ValidateAntiForgeryToken属性会阻止数据在传递给 MVC Controller 时被正确解析,当我删除ValidateAntiForgeryToken属性但不能使用它时,下面的代码有效,controller 动作中的所有参数都被传递,除了翻译数组。

Please advise on how to pass array of objects while utilizing ValidateAntiForgeryToken attribute, is it even possible?请告知如何在使用ValidateAntiForgeryToken属性时传递对象数组,这是否可能?

This is my code这是我的代码

C# C#

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

CommentTranslation is评论翻译是

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

js 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;
},

Ended up using FormCollection that you can just generically pass anything into the Controller.最终使用FormCollection ,您可以将任何东西一般地传递到 Controller 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValidateAntiforgeryToken 不适用于 ASP.NET MVC 中的 Ajax - ValidateAntiforgeryToken not working with Ajax in ASP.NET MVC 在asp.net mvc 3中使用ajax发送一个json对象数组 - Sending an array of json objects to action with ajax in asp.net mvc 3 如何在asp.net中使用ajax将数组传递给mvc - how to pass array to mvc using ajax in asp.net 通过AJAX发送一个对象数组 - ASP.NET MVC - Sending an array of objects via AJAX - ASP.NET MVC 在 ASP.NET Core 中将 ValidateAntiForgeryToken 属性与 Unobtrusive Ajax 插件一起使用 - Using ValidateAntiForgeryToken attribute with Unobtrusive Ajax plugin in ASP.NET Core 如何将多个Json对象传递给ASP.net MVC控制器? - How can I pass multiple Json objects to ASP.net MVC controller? 如何在 Visual Studio 2022 / ASP.NET Core 5.0 MVC 中使用 Ajax? - How to use Ajax in Visual Studio 2022 / ASP.NET Core 5.0 MVC? 如何使用JSON,jQuery向ASP.NET MVC Controller发布复杂对象数组? - How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller? javascript-如何将JSON对象中的数组从JavaScript传递给asp.net MVC控制器方法? - How to pass array in a JSON object from javascript to asp.net mvc controller method? 如何在MVC asp.net中将LIST传递为JSON查看 - How to pass LIST to view as JSON in MVC asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM