我有ajax请求将数据发送到我的控制器,它会收集我的下拉列表的值
错误是
POST http://localhost:65070/form/create 500 (Internal Server Error)
错误的反应是
The required anti-forgery form field "__RequestVerificationToken" is not present.
更新 我的表格
@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>
}
我的ajax请求
$('#State').change(function () {
var a = $('#State').val();
$.ajax({
url: "/form/create",
type: "POST",
data: { 'SubID': a },
success: function (result) {
// console.log(result);
}
});
});
我的控制器
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);
}
它给出了500个内部错误..它的尴尬plz帮助
答案 0 :(得分:10)
您的帖子方法必须具有[ValidateAntiForgeryToken]
属性。删除属性或在视图中添加令牌
@Html.AntiForgeryToken()
并将其传回ajax函数
$('#State').change(function () {
var a = $('#State').val();
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
....
data: { __RequestVerificationToken: token, 'SubID': a },
....
请注意,操作方法
中不需要form
参数
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(int SubID)
{
....
答案 1 :(得分:0)
我有一个在我的应用程序中使用过的示例。在控制器中,您必须放置
@Autowired
AppService appService;
@Autowired
AppDao ds;
@Qualifier("nameParamJdbcTemplate")
@Autowired
public NamedParameterJdbcTemplate nameParamJdbcTemplate;
I am new to spring so any help or working examples would be great. Thanks
在视图中,我有一个ajax函数
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Verificar(int idempleado, string desde, string hasta)
{
...
return Json(suiche, JsonRequestBehavior.AllowGet);
}
在表单中,我使用 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");
}
});
}