In my web application I have added autocomplete JavaScript function to a textbox to get the data while typing and show it to the user.
Here when the label is clicked, the Textbox takes the selected item name to the textbox.
So in the controller, I am taking the Item Id, Item Name, and the Qty for the typed value.
There is another input element in the view and What I want is, when the user selects a item from the searched label, that selected item's Qty I want to pass to the id = "ItemAvaQty"
Need help doing that.
This is my code.
In the view.
<div class="row">
<div class="col-md-6">
<input type="text" placeholder="Type the Item Name or Number Here" class="form-control" id="itemSearchBox" />
</div>
<div class="col-md-1">
<input type="text" placeholder="0" disabled class="form-control" id="ItemAvaQty" />
</div>
<div class="col-md-2">
<button id="addItem" type="button" class="btn btn-primary">Add</button>
</div>
</div>
This is the JavaScript
$("#itemSearchBox").keyup(function () {
$("#itemSearchBox").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetSearchItems","Ajax")',
type: "POST",
dataType: "json",
data: { search: $("#itemSearchBox").val() },
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Name,
value: item.Name,
};
}))
},
error: function (xhr, status, error) {
}
})
},
});
});
Controller
public JsonResult GetSearchItems(string search) {
int CompanyID = int.Parse(((System.Security.Claims.ClaimsIdentity) User.Identity).FindFirst("CompanyID").Value);
var searchItems = (from e in db.Items join i in db.InventoryModel on e.ID equals i.Item_ID join l in db.CompanyLocations on i.Location_ID equals l.ID where e.CompanyID == CompanyID &&
e.Status == true &&
e.ItemName_Number.Contains(search) select new {
ID = e.ID,
Name = e.ItemName_Number + " " + l.LocationName,
Qty = i.Ava_Qty
}).ToList();
return new JsonResult {
Data = searchItems, JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}