后置500(内部服务器错误)AJAX,MVC [英] POST 500 (Internal Server Error) ajax,mvc

查看:189
本文介绍了后置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)
{
  ....

这篇关于后置500(内部服务器错误)AJAX,MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆