1

I am returning array of strings from controller to ajax call. trying to set to textbox those values. in textbox it is not populating. but I can see data in success method.

  [HttpGet]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public JsonResult GetWorkNamesAutoPoplate(string companyName)
    {
        ...
        var newKeys = companyNameslst.Select(x => new string[] { x }).ToArray();

        var json = JsonConvert.SerializeObject(newKeys);
        return Json(json, JsonRequestBehavior.AllowGet);
    }

JS

 $(document).on('change', '[name="FindCompanyName"]', function () {
     $('[name="FindCompanyName"]').autocomplete({        
        source: function (request, response) {           
            $.ajax({
                url: "GetWorkNamesAutoPoplate",
                type: "GET",
                dataType: "json",
                data: { companyName: $('[name="FindCompanyName"]').val() },
                success: function (data) {
                    alert(JSON.stringify(data));
                    response($.map(data, function(item) {
                        console.log(item);
                        return {
                            value: item
                        }
                    }));    
                }
            });
        },
        messages: {
            noResults: "", results: ""
        }    
    });
 });

alert(JSON.stringify(data)); display like this. enter image description here

How to populate this data in textbox

1

2 Answers 2

0

The return type of your json is an array of arrays, i think you should return it as an array

var newKeys = companyNameslst.ToArray();

also, your data are serialized twice,

one from line,

var json = JsonConvert.SerializeObject(newKeys);

and second time from JsonResult action filter

return Json(json, JsonRequestBehavior.AllowGet);

sending json data like,

return Json(newKeys, JsonRequestBehavior.AllowGet);

instead of

var json = JsonConvert.SerializeObject(newKeys);
return Json(json, JsonRequestBehavior.AllowGet);

should work.

hope this helps.

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

Comments

0

This may resolve your issue :

success: function (data) {
                  alert(JSON.stringify(data));
                  if (data != null) {
                    response(data.d);
                   }
                }

Also this link might help you to get some information :How to use source: function()... and AJAX in JQuery UI autocomplete

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.