Example is different but it works.
Here the situation is, I have to make autocomplete to work with the preset options as shown in the image. I have to input both options and entry I type-in.
Autocomplete with Preselect option,

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function () {
$('#SearchBasedFunction').autocomplete({
source: ["1", "2"],
minLength: 0
}).focus(function () {
var FunctionType = $('#SearchOption').val();
$(this).autocomplete({
source: function (request, response) {
$.ajax({
url: '/SalesModule/Sales/SearchNameAutoCompleteData',
type: "POST",
dataType: "json",
data: { Prefix: request.term, FunctionType },
success: function (data) {
//console.log('track ....');
//console.log(data);
response($.map(data, function (item) {
// response(data.d);
return { label: item, value: item };
}))
$('#ActionSet2').val("BOMItems");
}
})
},
messages: {
noResults: "", result: ""
}
});
});
});
</script>
Below is the code for the SearchNameAutoCompleteData (to be written in controller)
public JsonResult SearchNameAutoCompleteData(string Prefix, string FunctionType)
{
List<string> result = new List<string>();
string constr = ConfigurationManager.ConnectionStrings["DhanyaContext"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT SystemFunction from tbl_SystemFunctions where SystemFunction LIKE '%'+@Prefix+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Prefix", Prefix);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["SystemFunction"].ToString());
}
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}