Reputation: 2011
I am using jQuery-Autocomplete for one of my forms: https://github.com/devbridge/jQuery-Autocomplete
I am receiving an error because I believe my response is in the wrong format.
The response from the server must be in this format:
"suggestions": [
{ "value": "United Arab Emirates", "data": "AE" },
{ "value": "United Kingdom", "data": "UK" },
{ "value": "United States", "data": "US" }
]
My response looks like: (It's missing "suggestions:")
[
{ "value": "United Arab Emirates", "data": "AE" },
{ "value": "United Kingdom", "data": "UK" },
{ "value": "United States", "data": "US" }
]
My action looks like:
[AjaxRequest]
public JsonResult UserLookup(string query)
{
var users = _userRepo
.GetUsers(query)
.Select(u => new { Value = u.UserId, Data = u.FullName });
return Json(users, JsonRequestBehavior.AllowGet);
}
Ajax Request:
<script>
$(document).ready(function () {
$('.requestor-autocomplete').autocomplete({
serviceUrl: '@Url.Action("userlookup", "pir")',
minChars: 3
});
});
</script>
Any suggestions on how I can format the response from the server correctly?
Upvotes: 1
Views: 335
Reputation: 2011
Thanks to @SkelDave I was able to figure out what I needed to do. I updated the last line in my action to: return Json(new { Suggestions = users }, JsonRequestBehavior.AllowGet);
Upvotes: 1