0

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.

KJSR
  • 1,679
  • 6
  • 28
  • 51
nil
  • 145
  • 1
  • 11
  • @JonathonChase already gone through the link it says create form and put the @Html.AntiForgeryToken(). My code already have this and also the problem is for partial view. – nil Jul 25 '19 at 14:46
  • 3
    You're using `Html.BeginForm`, while the duplicate suggests `@Ajax.BeginForm`. Have you tried changing to the Ajax form? – Jonathon Chase Jul 25 '19 at 14:47
  • Thanks it solved my issue. – nil Jul 26 '19 at 09:28

0 Answers0