So I have a partialview and I have 2 forms inside it as below:
@using (Html.BeginForm("AddAlbum", "Admin", FormMethod.Post, htmlAttributes: new { id = "frmAlbumAdd", novalidate = "novalidate", autocomplete = "off" }))
{
@Html.AntiForgeryToken()
<!--some controls and submit button-->
}
.....
.....
@using (Html.BeginForm("UploadImages", "Admin", FormMethod.Post, htmlAttributes: new { id = "frmUploadImages", novalidate = "novalidate", autocomplete = "off", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<!--some controls and submit button-->
}
and I am doing ajax post to the Admin controller as below:
[HttpPost]
[ValidateAntiForgeryToken]//This works well
public JsonResult AddAlbum(AlbumDataModel model)
{
//perform some task and return result
}
[HttpPost]
[ValidateAntiForgeryToken]//This results in Error
public JsonResult UploadImages([Bind(Prefix = "UIAModel")] UploadImageAlbum model)
{
//perform some task and return result
}
and the error I get on second form submission is "The required anti-forgery form field \"__RequestVerificationToken\" is not present."
According to this post in SO we can have antiforgerytokens for different forms individually. But am not sure why this turns out to be error.
I've also tried adding @Html.AntiForgeryToken() in the Layout where partialviews load and excluded it from forms and had below ajaxSetup to send AntiForgeryToken but even that didn't work.
$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
var verificationToken = $("meta[name='__RequestVerificationToken']").attr('content');
if (verificationToken) {
jqXHR.setRequestHeader("X-Request-Verification-Token", verificationToken);
}
});
How can I overcome this issue? What is actually happening here?
UPDATE:
I am using ajax to post the formdata to controller as below:
$.ajax({
url: url,
type: "POST",
dataType: 'json',
data: formData,
processData: false,
contentType: false,
success: function (data) {
}
});