Reputation: 1396
On key entry in autocomplete box, I am returning Key,Value pair from MVC controller in JSON format.
public ActionResult UserNameAutoComplete(string term)
{
DBEntities db = new DBEntities();
...codes to get data from database
jsonString += jSearializer.Serialize(userList);
return Json(jsonString, JsonRequestBehavior.AllowGet);
}
the retruned Json String is [{"UserId":"1","UserName":"admin"},{"UserId":"3","UserName":"newtonsheikh"}]
In the View i am getting this
The jquery is
$("#Username").autocomplete({
source: '@Url.Action("UserNameAutoComplete")'
});
My question is how do i parse this returned json? The expected output is
Upvotes: 1
Views: 5879
Reputation: 1396
solved the issue. i had to change my jquery code. There is no need to change the code for returning the json string.
$("#Username").autocomplete({
select: function (e, ui) {
$("#Username").val(ui.item.label);
return false;
},
source: function (request, response) {
$.ajax({
url: '@Url.Action("UserNameAutoComplete")',
data: request,
success: function (data) {
var ParsedObject = $.parseJSON(data);
response($.map(ParsedObject, function (item) {
return {
label: item.UserName,
value: item.UserId
};
}))
}
});
}
});
Upvotes: 6
Reputation: 2284
You trying to serialize your dataSet twice. You need use
return Json(jsonString, JsonRequestBehavior.AllowGet);
without(!) jSerializer.
or if you want do this with serializer try return just content of your serialized dataset
jsonString += jSearializer.Serialize(userList);
return Content(jsonString);
Upvotes: 0