Reputation: 1500
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 (Internal Server Error)
response of error is
The required anti-forgery form field "__RequestVerificationToken" is not present.
UPDATE My Form
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Form</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FormName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FormName)
@Html.ValidationMessageFor(model => model.FormName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MasterID, "MasterModule")
</div>
<div class="editor-field">
@Html.DropDownList("MasterID", String.Empty)
@Html.ValidationMessageFor(model => model.MasterID)
</div>
<select id="State" name="state"></select><br />
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
My ajax Request
$('#State').change(function () {
var a = $('#State').val();
$.ajax({
url: "/form/create",
type: "POST",
data: { 'SubID': a },
success: function (result) {
// console.log(result);
}
});
});
My controller
public ActionResult Create(Form form, int SubID)
{
if (ModelState.IsValid)
{
form.SubId =SubID;
form.CreatedDate = DateTime.Now;
form.CreatedBy = 1;
form.CreatedDate = DateTime.Now;
form.IsActive = true;
form.ModifyBy = 1;
form.ModifyDate = DateTime.Now;
db.Forms.Add(form);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.MasterID = new SelectList(db.Departments, "MasterId", "ModuleName", form.MasterID);
return View(form);
}
It is giving 500 internal error.. its awkward plz help
Upvotes: 5
Views: 31677
Reputation: 1
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()
Upvotes: 0
Reputation:
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)
{
....
Upvotes: 14