anti forgery error

Topics: Writing modules
Aug 1, 2012 at 4:09 PM

Hey everyone,

I've seen this error pass a few times on this forum but I can't seem to find the solution...

I have a search function: this is the view:

@using CLB.SchoolModule.Models
@using System.Linq
@using System.Collections
@model CLB.SchoolModule.ViewModels.ZoekViewModel
@using (Html.BeginForm("Toon_Medewerker", "Zoek")) {
Ik ben op zoek naar een medewerker:
@Html.DropDownListFor(model => model.MedewerkerID, new SelectList((from s in Model.AllMedewerkers select new { Id = s.Id, FullName = s.AchterNaam + " " + s.VoorNaam }), "Id", "Fullname"), "Selecteer een medewerker")
<input type="submit" value="Toon" /> </fieldset> } @using (Html.BeginForm("Toon_School", "Zoek")) { @Html.ValidationSummary(true) <fieldset>
Ik ben op zoek naar een school:
In Stad:
@Html.DropDownListFor(model => model.StadId, new SelectList((from s in Model.AllStadRecords select new { StadId = s.Id, Naam = s.Naam }), "StadId", "Naam"), "selecteer een stad")
@Html.DropDownListFor(model => model.SchoolTypeId, new SelectList((from s in Model.AllSchoolTypeRecords select new { SchoolTypeId = s.Id, SchoolType = s.SchoolType }), "SchoolTypeId", "SchoolType"), "selecteer een type")
<input type="submit" value="Toon" /> </fieldset> }
From time to time I get the error:

A required anti-forgery token was not supplied or was invalid.

When I change the code to  @using (Html.beginformantiforgerypost("Toon_Medewerker", "Zoek")) { ...

I get a compilation error:

'System.Web.Mvc.HtmlHelper<CLB.SchoolModule.ViewModels.ZoekViewModel>' does not contain a definition for 'BeginFormAntiForgeryPost' and the best extension method overload


What can I do to solve this?

Aug 2, 2012 at 12:18 AM

Could you paste in the complete compilation error? It's cut off: "'System.Web.Mvc.HtmlHelper<CLB.SchoolModule.ViewModels.ZoekViewModel>' does not contain a definition for 'BeginFormAntiForgeryPost' and the best extension method overload...???"

Aug 2, 2012 at 9:28 AM


Sorry, this is the complete error:

My Documents\Visual Studio 2010\Projects\CLB_1_5\src\Orchard.Web\Modules\CLB.SchoolModule\Views\zoek.cshtml(6): error CS1928: 'System.Web.Mvc.HtmlHelper<CLB.SchoolModule.ViewModels.ZoekViewModel>' does not contain a definition for 'BeginFormAntiForgeryPost' and the best extension method overload 'Orchard.Mvc.Html.HtmlHelperExtensions.BeginFormAntiForgeryPost(System.Web.Mvc.HtmlHelper, string, System.Web.Mvc.FormMethod)' has some invalid arguments

Aug 2, 2012 at 11:00 AM
Edited Aug 2, 2012 at 11:01 AM

Ok, as the message suggests, you are passing in arguments to BeginFormAntiForgeryPost that are not expected (i.e. incorrect number of arguments or arguments of the wrong type. Also make sure you have the casing correct).
Looking at the signature, the first argument takes a string, the second one a FormMethod. So you probably want to invoke the method as follows:

@using (Html.BeginFormAntiForgeryPost(Url.Action("Toon_Medewerker", "Zoek"))) { ...


Aug 2, 2012 at 11:45 AM


That did it! As always, thanks!