I'm posting data from the partial view to perform update operation
using MVC @Ajax.ActionLink
method, but while including the tag
[ValidateAntiForgeryToken]
it is giving me the error.
It is working fine when I remove the tag [ValidateAntiForgeryToken]
from action method. My view already had one Antiforgery token. I
tried removing @Html.AntiForgeryToken()
from partial view but still
got the same error.
_partialView.csHtml code :
@using (Html.BeginForm("Action", "Controller"))
{
@Html.AntiForgeryToken()
@Html.Hidden("hdnID", "", new { @id = "hdnID" })
@Ajax.ActionLink(" ", "Action", "Controller", new { ID = "Ids" },
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divLinkedUnlinkedVLans",
LoadingElementId = "progress"
}, new { @id = "btnRight", @class = "btn btn-default fa fa-caret-right fa-4x center" })
}
Controller Code :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Action(string ID)
{
try
{
// Some database operation
}
catch (Exception)
{
throw;
}
}
Partial View call from Controller :
[HttpGet]
public ActionResult ActionName()
{
return PartialView("_partialView", Data object having data from database);
}
When the button is clicked the form is posted back and it is giving the error message as '__requestverificationtoken is not present'. Help me with posting the antiforgery token to controller so as I can avoid CSRF attacks.