【问题标题】:Ajax GET request calling POST instead (ASP.NET MVC5)Ajax GET 请求调用 POST (ASP.NET MVC5)
【发布时间】:2014-05-30 07:56:56
【问题描述】:

我创建了一个 ajax GET 调用来执行搜索功能。但是每次单击搜索按钮时,它都会调用 POST(从而为模型返回 null 错误)。我不确定我做错了什么。请帮忙?

我的控制器:

    //
    // GET: /DataEntry/ChargeBack
    public ActionResult ChargeBack(string dwName, string searchTerm = null)
    {
        var model = createModel();

        if (Request.IsAjaxRequest())
        {
            return PartialView("_Suppliers", model);
        }

        return View(model);
    }

    //
    // POST: /DataEntry/ChargeBack
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ChargeBack(ChargeBackViewModel model)
    {
        if (ModelState.IsValid)
        {
            model.someAction();
        }
        return View(model);
    }

我的主视图:

    @model CorporateM10.Models.ChargeBackViewModel

    @using (Ajax.BeginForm(
            new AjaxOptions
            {
                HttpMethod = "get",
                InsertionMode = InsertionMode.Replace,
                UpdateTargetId = "SuppliersList"
            }))
    {
        @Html.AntiForgeryToken()
        <input type="search" name="searchTerm" class="form-control col-md-2" />
        <input type="submit" value="Search by Name" class="btn btn-info" />
    }

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            @Html.ValidationSummary(true)

            @Html.Partial("_Suppliers", Model)

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

我的部分观点:

@model CorporateM10.Models.ChargeBackViewModel

<div class="form-group" id="SuppliersList">
    <div class="col-md-10">
        @Html.EditorFor(model => model.Suppliers)
        @Html.ValidationMessageFor(model => model.Suppliers)
    </div>
</div> 

【问题讨论】:

  • 是什么让你认为它在做 POST?
  • 改变 type="submit" by type="button" ?
  • @DavidG,它正在尝试使用模型 ChargeBackViewModel 调用 post 方法。我得到模型为空的错误。如果它调用 GET,就不会出现这样的错误。
  • @OuSs,感谢您的建议。我尝试将其更改为“按钮”。这次不是调用post,而是出现了另一个错误:Error。处理您的请求时出错。

标签: ajax asp.net-mvc-5


【解决方案1】:

我找到了。这是由于不包括 jquery.unobtrusive-ajax 库引起的。一旦包含在内,Ajax 操作就会按预期工作。

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    相关资源
    最近更新 更多