Sony
Sony

Reputation: 7186

Ajax form not submitting to controller action

Ajax form is not submitting to controller action. Here is the code

@using (Ajax.BeginForm("searchCustomers", "Transaction", new { phoneNumber = Model.CustomerMobile }, new AjaxOptions
                {
                    UpdateTargetId = "custList",
                    InsertionMode = InsertionMode.Replace
                }))
                {
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Customer Mobile No:</label>
                            @Html.TextBoxFor(x => x.CustomerMobile, new { @class = "form-control", id = "custMobile" })
                        </div>
                        @*<div class="form-group">
                            <label>Customer Name</label>
                            @Html.TextBoxFor(x => x.CustomerName, new { @class = "form-control", id = "custName" })
                        </div>*@
                        <input type="submit"  class="btn btn-default" value="Get Customer Details" >
                    </div>

                }

Here is the controller action

 public ActionResult searchCustomers(string phoneNumber)
    {
        if (phoneNumber==null)
        {
            return PartialView(new List<Models.Customer>());
        }

        var c = Database.Session.Query<Models.Customer>()
            .Where(x => x.MobileNumber.Like(phoneNumber) )
            .ToList();
        return PartialView(c);

    }

but the ajax form is not submitting. I've added the JavaScript files as bundles. I've another @Html.Action("searchCustomers", new { phoneNumber = Model.CustomerMobile }) this one calls the controller action.

Upvotes: 0

Views: 398

Answers (1)

Anadi
Anadi

Reputation: 754

Everything is fine in your code. There are two javascript files that are needed for Ajax.Beginform to work.

  1. jquery-{Vaersion}.js
  2. jquery.unobtrusive-ajax.js

Check whether you have included those files to your view or not. Or if your view has any LayOut if those javascript files are included in your LayOut or not.

Upvotes: 1

Related Questions