ASP.NET MVC 5 + jQuery UI Autocomplete + Web API
JSON Format |
{ "1" : "Select Value … …", "2" : "Select Value … …", "3" : "Select Value … …", … …, … … } |
Web API |
using Newtonsoft.Json.Linq; public class ValuesController : ApiController { public JObject Get(string term) { JObject returnobj = new JObject(); /* term parameter – is the Value from the Web Interface TextBox ( AutoComplete ). – is passed from jQuery UI AutoComplete function & AJAX GET. */ // Perform the Record Filtering Step from here & Prepare the Value on the JSON.NET JObject Variable return returnobj; // The Return JSON Object from Client Side would be as same as the above JSON Format. } |
Web & JavaScript ( jQuery ) |
<input id="search_txt" type="text" value="" style="… …" /> |
// Define the jQuery Java Script Library / jQuery UI Java Script Library & CSS Library first var url = "/api/values"; var id = "search_txt"; $(document).ready(function () { $("#" + id).autocomplete({ source: function (request, response) { //JSON.stringify(JSON.stringify(request)); $.ajax({ url: url, type: "GET", cache: false, data: request, dataType: "json", success: function (json) { response($.map(json, function (name, val) { return { label: name, value: val } })); }, error: function (XMLHttpRequest, textStatus, errorThrown) { console.log("Error."); } }); }, minLength: 2, select: function (event, ui) { $("#" + id).val(ui.item.label); return false; } }); }); |