In this article I will show you how to bind multiple values to autocomplete textbox using jQuery ajax post call to a controller and return json.
$("#txtCategory").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetPostContent", "submit", new { area = "" })',
data: "{'term': '" +request.term+"'}",
dataType: "json",
type: "POST",
contentType: "application/json;charset=utf-8",
//dataFilter:function (data) { return data; },
success: function (data) {
response($.map(data, function(item) {
return { label: item, value1: item };
}));
},
error: function (XMLHttpRequest, textStatus,errorThrown) {
alert(textStatus);
}
});
},
});
public JsonResult GetPostContent(string term)
{
// List<Category> Categories= db.Categories.Where(s => s.Name.StartsWith(term)).ToList();
var results = from c in db.Posts
wherec.Name.ToLower().StartsWith(term.ToLower())
select c.Name + " - " + c.Description;
JsonResult result = Json(results.ToList(), JsonRequetBehavior.AllowGet);
return result;
}
Output:
Post your comments / questions
Recent Article
- How to add two numbers in Android Studio? | Source Code
- FindViewByID returns null in android studio -SOLVED
- Saving changes is not permitted in SQL SERVER - [SOLVED]
- Restore of database failed. File cannot be restored over the existing. -[SOLVED]
- One or more projects in the solution were not loaded correctly in Visual Studio 2019 | FIXED
- How to find Laptop's Battery Health?
- SOLVED-Related Field got invalid lookup: icontains error in Django
- How to enable Search Feature in Django admin?
Related Article