I have ajax request that sends data to my controller , it collects value of my dropdown
the error is
POST http://localhost:65070/form/create 500 (Int
I have an example that I have used in my applications. In the controller, you must place
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Verificar(int idempleado, string desde, string hasta)
{
...
return Json(suiche, JsonRequestBehavior.AllowGet);
}
In the view, I have an ajax function
function Verificar(desde, hasta) {
var token = $('[name=__RequestVerificationToken]').val();
var empleadoId = parseInt($('#empleadoId').val());
var url = '/Settlements/Verificar';
var settings = {
"async": true,
"crossDomain": true,
"url": url,
"method": "POST",
"data": { __RequestVerificationToken: token, idempleado: empleadoId, desde: desde, hasta: hasta, }
}
$.ajax(settings).done(function (response) {
if (response) {
$('#mensajefrom').text(null);
$('#mensajeto').text(null);
} else {
$('#mensajefrom').text("There is another settlement with that date range");
$('#mensajeto').text("There is another settlement with that date range");
}
});
}
in the form, I use @Html.AntiForgeryToken()
Your post method must have the [ValidateAntiForgeryToken]
attribute. Either remove the attribute or in the view, add the token
@Html.AntiForgeryToken()
and pass it back in the ajax function
$('#State').change(function () {
var a = $('#State').val();
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
....
data: { __RequestVerificationToken: token, 'SubID': a },
....
Note the form
parameter is not necessary in the action method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(int SubID)
{
....