I have this view that brings in all data from an entity:
@model MyApplication.Application.TableModel<Entities.FPDrinkingWater>
@{
Layout = null;
var insertionMode = InsertionMode.Replace;
var fail = "displayFailure";
var target = "UnverifiedDrinkingWatersContent";
var ajax = "UnverifiedDrinkingWaterLogLoader";
var verify = Html.UserHasClaim("FP/DrinkingWater/VerifyDrinkingWater");
var action = "VerifyDrinkingWater";
string form = action + "Form";
}
<br />
@if (Model != null && Model.Alert != null && Model.Alert.Message != null)
{
@Html.Alert(Model.Alert)
}
@MyHelper.Loader(ajax)
<div class="form-group">
<table id="UnverifiedDrinkingWaterTable" class="table table-hover">
<thead>
<tr>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().SID)</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Location)</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Replicate)</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().CollectionDate)
</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().CollectionTime)
</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Collectors)</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Clorinated)</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Comments)</th>
<th>@Html.LabelFor(m => m.Data.FirstOrDefault().Verified)</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Data.Count(); i++)
{
using (Ajax.BeginForm(actionName: action, routeValues: null,
htmlAttributes: new { id = form, @class = "form-horizontal" },
ajaxOptions:
new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = target,
OnSuccess = "success",
LoadingElementId = ajax
}))
{
@Html.AntiForgeryToken()
<tr>
<td>@Html.DisplayFor(m => m.Data[i].SID)</td>
<td>@Html.DisplayFor(m => m.Data[i].Location)</td>
<td>@Html.DisplayFor(m => m.Data[i].Replicate)</td>
<td>@Html.DisplayFor(m => m.Data[i].CollectionDate)</td>
<td>@Html.DisplayFor(m => m.Data[i].CollectionTime)</td>
<td>@Html.DisplayFor(m => m.Data[i].Collectors)</td>
<td>@Html.DisplayFor(m => m.Data[i].Clorinated)</td>
<td>@Html.DisplayFor(m => m.Data[i].Comments)</td>
<td>@Html.CheckBoxFor(v => v.Data[i].Verified, new {
data_url = Url.Action("VerifyDrinkingWater", "DrinkingWater"), id =
"checkboxid" }) </td>
</tr>
}
}
</tbody>
</table>
</div>
<hr />
@if (Model.Data.Count > 0)
{
<script>
$(document).ready(function () {
makeDataTable('UnverifiedDrinkingWaterTable');
});
(function () {
$('#checkboxid').change(function () {
var data = {};
data[$(this).attr('name')] = $(this).is('checked');
$.ajax({
url: $(this).data('url'),
type: 'POST',
data: data,
success: function (result) {
}
});
});
});
</script>
}
What my goal is that I want to update 3 fields on the record displayed in the table by checking the "Verified" checkbox. I followed This Link to enable a click event on the checkboxfor. However, I receive an error claiming that The required anti-forgery form field __RequestVerificationToken is not present.
. This is the only error I receive.
So my question is, how can I combine the AntiForgeryToken creation by the Ajax call AND pass the checked status of the checkbox at the same time and then pass it to the controller? Right now it's not even touching the controller and just giving me the AntiForgeryToken Error.