改为调用POST的Ajax GET请求(ASP.NET MVC5)

时间:2014-05-30 07:56:56

标签: ajax asp.net-mvc-5

我已经创建了一个执行搜索功能的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> 

1 个答案:

答案 0 :(得分:0)

我找到了它。它是由不包括jquery.unobtrusive-ajax库引起的。一旦包含,Ajax操作就可以正常工作。