Postback on Changing DropDownLists gives error "The required anti-forgery form field &__RequestVerificationToken; is not present."

Topics: Writing modules
Apr 8, 2013 at 4:00 PM
I am using ajax call for calling the controller method.
I have 3 dropdownlists.Based on their selection I have to narrow the results displayed..
<form class="articleFilterForm" method="post" action="">

    <label>

        Industry</label>

    <div class="customSelect">

        <div class="selectValue">

            All</div>
         
      @Html.DropDownListFor(model => model.IndustryId,
                      Model.Industries.Select(s => new SelectListItem
                      {
                          Selected = s.Id == Model.IndustryId,
                          Text = s.IndustryTitle,
                          Value = s.Id.ToString()
                      }),
                         "Choose an Industry...", new
                         {
                             onchange = "FilterInfo()"
                         })
                        

    </div>
</div>
<div class="row selectBoxBg">
    <label>
        Type</label>
    <div class="customSelect">
        <div class="selectValue">
            All</div>


 @Html.DropDownListFor(model => model.DocumentTypeId,
                      Model.DocumentTypes.Select(s => new SelectListItem
                      {
                          Selected = s.Id == Model.DocumentTypeId,
                          Text = s.DocumentTypeTitle,
                          Value = s.Id.ToString()
                      }),
                          "Choose a Document Type...", new
                          {
                              onchange = "FilterInfo()"
                          })
                         
                           
    </div>
</div>
<div class="row selectBoxBg">
    <label>
        State</label>
    <div class="customSelect">
        <div class="selectValue">
            All</div>
        @Html.DropDownListFor(model => model.StateId,
                      Model.States.Select(s => new SelectListItem
                      {
                          Selected = s.Id == Model.StateId,
                          Text = s.StateTitle,
                          Value = s.Id.ToString()
                      }),
                      "Choose a State...", new
                          {
                              onchange = "FilterInfo()"
                          })
                  

    </div>
</div>


     
   
</form>



<script type="text/javascript">

    function FilterInfo() {


        var Inid = document.getElementById("ResourceLibrary_IndustryId").value;
 var Stid = document.getElementById("ResourceLibrary_StateId").value;
 var Docid = document.getElementById("ResourceLibrary_DocumentTypeId").value;


        $.ajax({
            type: "POST",
            
            url: "/OrchardLocal/ResourceLibrary/ResourceLibrary/FilterData",
            data: { StateId: 'Inid', IndustryId: 'Stid',DocTypeId:'Docid' },
            dataType: "json",
            success: function (response) { alert(response); },
            error: function (xhr, ajaxOptions, thrownError) { alert(xhr.responseText); }
        });
 
    }
</script>
View :

Controller Name:ResourceLibrary
MethodName:FilterData

I have tried using @html.BeginFormAntiForgeryPost("FilterData", FormMethod.Post,new { area = "ResourceLibrary" })

still the same exception

HttpAntiForgeryException (0x80004005): The required anti-forgery form field "__RequestVerificationToken" is not present.]

System.Web.Helpers.AntiXsrf.TokenValidator.ValidateTokens(HttpContextBase httpContext, IIdentity identity, AntiForgeryToken sessionToken, AntiForgeryToken fieldToken) +258457

System.Web.Helpers.AntiXsrf.AntiForgeryWorker.Validate(HttpContextBase httpContext) +71

System.Web.Helpers.AntiForgery.Validate() +119

System.Web.Mvc.ValidateAntiForgeryTokenAttribute.OnAuthorization(AuthorizationContext filterContext) +24

Orchard.Mvc.AntiForgery.AntiForgeryAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext) in D:\softwares\Orchard.Source.1.6\src\Orchard\Mvc\AntiForgery\AntiForgeryAuthorizationFilter.cs:36

System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList1 filters, ActionDescriptor actionDescriptor) +102

System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343

System.Web.Mvc.&lt;&gt;c__DisplayClass1d.&lt;BeginExecuteCore&gt;b__19() +24

System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21

System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8
1.<BeginSynchronous>b__7(IAsyncResult _) +12

System.Web.Mvc.Async.WrappedAsyncResult1.End() +62

System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +61

System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +25

System.Web.Mvc.Async.WrappedAsyncResult
1.End() +62

System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +49

System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10

System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +28

System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25

System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62

System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +49

System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9

Orchard.Mvc.Routes.HttpAsyncHandler.EndProcessRequest(IAsyncResult result) in D:\softwares\Orchard.Source.1.6\src\Orchard\Mvc\Routes\ShellRoute.cs:162

System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8967981

System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Developer
Apr 10, 2013 at 11:10 AM
It looks like you're not submitting the antiforgery token as part of your AJAX request. I believe there are a couple of posts on this forum that show how to do it.