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 create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article