[英]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.