0

I'm trying to create bespoke lists in my cshtml file using an ajax call

.ajax({
        type: "GET",
        cache: false,
        url: "@Url.Action("getValidationLists")",
        contentType: "application/json",
        dataType: "json",
        success: function (result) {                
            var list = result.list;
            $.each(list, function (index, value) {
                alert(value);
            });
        }
    });       

The Controller then collects the data puts it in a List and returns it to my cshtml file using json format. the controller method is below

[HttpGet]
[Authorize]
public JsonResult getValidationLists()
{
    List<List<String>> validList = new List<List<String>>();
    List<UIReferenceData> results = AddErrorsToModelState<List<UIReferenceData>>(_StaticDataServiceAgent.GetAllAssetTypes());
    for (int i = 0; i < results.Count; i++)
    {
        List<String> resultList = new List<String>();
        string id = results[i].ID.ToString();
        string name = results[i].Name;
        resultList.Add(id);
        resultList.Add(name);
        validList.Add(resultList);
    }
    return Json(new
    {
        validList = validList
    }, JsonRequestBehavior.AllowGet);
}

I've tried several different versions but the returning code never gets as far as the alert(value); code. What am i missing?

3
  • Can you debug the controller method? Can you intercept the call? Commented Dec 2, 2015 at 16:36
  • Yes, as far as the return and the List<string> validList has the correct data Commented Dec 2, 2015 at 16:39
  • Have you verified you are hitting your controller method and that it is not throwing a 500? If you are getting data back but you are not making it to the $.each it may be that you need to parse the result first: var list = JSON.parse(result); Commented Dec 2, 2015 at 16:47

2 Answers 2

1

The property name is inconsistent with what you are returning from your action:

var list = result.list;

Should be:

var list = result.validList;

However you are returning a List<List<String>> rather than a List<String> too.

I think you need to address both of the above in order to fix.

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

Comments

0

The JSON response you are returning from your action method looks like this

{
    "validList": [      
        ["1", "SomeText"],
        ["2", "SomeText"],
        ["3", " SomeText"]
    ]
}

Your response has a validList property which is an array and each item in an array is again another array with 2 items.

This should work

 success: function (result) {           
     $.each(result.validList, function (index, value) {
       console.log(value[0]);
       console.log(value[1]);
    });
 }

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.