0

I am new to MVC. I am trying to create an order for a customer, so basically the person will selelct the customer from a list of phone numbers. So we need to ask the customer their phone number and as we type the number a list of numbers should be in a list below (AJAX). I have added the AJAX code but when I start typing numbers in my view I get a "Not Found" error.

This is my view screen as you can see the error

This is the code for the text box

         <div class="form-group">
                @Html.LabelFor(model => model.CustomerID, "Customer Cell", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10" onkeyup="Autocomplete()" onselect="">

                   @Html.EditorFor(model => model.CUSTOMER.customer_cell, new { htmlAttributes = new { @class = "autocomplete form-control", id="customercell" } })
                    @Html.ValidationMessageFor(model => model.CustomerID, "", new { @class = "text-danger" })


                </div>
            </div>
        </div>

This is the code for my AutoComplete function

<script>
    function Autocomplete() {


        var number = document.getElementById('customercell').value;

        $.ajax({
            type: 'GET',
            url: "AutoComplete.cshtml",
            data: "Customernumber=" + number,

            dataType: "json",
            //contentType: "application/json",
            success: function (html) {

                $(".autocomplete").autocomplete({
                    source: html
                });

            },

            error: function (XMLHttpRequest, textstatus, error) {

                alert(error);
            }
        });
    };
</script>

This is my AutoComplete class:

@{
    var db = Database.Open("ThruppsGrocersContext");
    var sql = "SELECT CUSTOMER.customer_cell FROM CUSTOMER WHERE CUSTOMER.customer_cell LIKE @0";
    var term = Request["Customernumber"] + "%";
    var result = db.Query(sql, term);
    var data = result.Select(p => new { label = p.CUSTOMER.customer_cell});
    Json.Write(data, Response.Output);
}
Santanu Sahoo
  • 1,137
  • 11
  • 29
Michael Fung
  • 129
  • 13
  • 3
    You are using it wrong. Re-read the autocomplete documentation and see the examples – mplungjan Aug 27 '15 at 07:29
  • Perhaps this: http://stackoverflow.com/questions/21385892/how-to-use-source-function-and-ajax-in-jquery-ui-autocomplete or http://stackoverflow.com/questions/11729588/understanding-and-implementing-jquery-autocomplete-with-ajax-source-and-appendto – mplungjan Aug 27 '15 at 07:49
  • `url` in `AJAX` call should be `controller/ActionMethod` and not the `cshtml` page. – Akshay Aug 27 '15 at 08:19

1 Answers1

0

change ajax data, may be fix

data: {Customernumber : number}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – SuperBiasedMan Aug 27 '15 at 10:36