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 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);
}