后置500(内部服务器错误)AJAX,MVC
问题描述:
我将数据发送到我的控制器Ajax请求,它收集我的下拉值
I have ajax request that sends data to my controller , it collects value of my dropdown
误差
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请求
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);
}
});
});
我控制器
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帮助
It is giving 500 internal error.. its awkward plz help
答
您交的方法必须具有 [ValidateAntiForgeryToken]
属性。要么删除属性或视图中添加标记
Your post method must have the [ValidateAntiForgeryToken]
attribute. Either remove the attribute or in the view, add the token
@Html.AntiForgeryToken()
和其传回的AJAX功能
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)
{
....