1

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

enter image description here

The jquery is

$("#Username").autocomplete({
    source: '@Url.Action("UserNameAutoComplete")'
});

My question is how do i parse this returned json? The expected output is enter image description here

1
  • System.Web.Script.Serialization.JavaScriptSerializer jSearializer = new System.Web.Script.Serialization.JavaScriptSerializer(); object of this class Commented Jun 27, 2013 at 11:52

2 Answers 2

6

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
                        };

                    }))
                }
            });
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.