1

I have created a view where when the user selects 2 of the dropdown lists values, an AJAX method is kicked off to populate an additional dropdown list's values

The callback triggers, however I cannot work with the response.

I am probably doing more than one thing wrong, so any help would be greatly appreciated. I am very new to Asp.Net core mvc

My Controller code (simplified - parameters are being used and are working):

public IActionResult GetCodes(string location, string xType)
{
    return Json(new Dictionary<string, string> {
    { "","" },
    { "Option1","120" },
    { "Option2","123" }
    });        
}

My jquery script on the view:

var ddl1val = $("#Location :selected").val().toLowerCase();
var ddl2val = $("#xType:selected").val().toLowerCase();
$.ajax({
    type: "GET",
    url: "/Ppl/GetCodes",          
    dataType: 'json',
    data: { location: ddl1val, xtype: ddl2val},
    success: function (data) {
        $("#ddlOptions").empty();

        var ops = '<option value=""></option>';

        alert(data); //returns [object Object]
        alert(data.Key); //returns undefined???

        //I need this to work
        for (var i = 0; i < data.length; i++) {
            ops += '<option value="' + data[i].Key + '">' + data[i].Value + '</option>';
        $("#ddlOptions").html(ops);
    }
}

console.log(data) shows:

{"": "", Option1: "120", Option2: "123"} 

I am not restricted to use JSON return, but it is lighter than xml, so I would prefer using it

3
  • 3
    I cannot work with the response. Please show us an example response Commented Sep 25, 2019 at 9:22
  • console.log(data) shows {"": "", Option1: "120", Option2: "123"} Commented Sep 25, 2019 at 9:27
  • Please edit that into your question. Commented Sep 25, 2019 at 9:38

1 Answer 1

2

Serialising a Dictionary<> to JSON will result in an object like this in your JS:

{
  "": "",
  "Option1": "120",
  "Option2": "123"
}

As such you should not use a for loop to enumerate it. As you're using jQuery you can use $.map() instead. Also note that you don't need to add the empty option as you include that in the response from the MVC endpoint. You also don't need to call empty() as you're overwriting the HTML of the select completely. Try this:

var data = {
  "": "",
  "Option1": "120",
  "Option2": "123"
}

// inside the success callback:
var ops = $.map(data, function(v, k) {
  return '<option value="' + v + '">' + k + '</option>';
});
$("#ddlOptions").html(ops);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="ddlOptions"></select>

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

1 Comment

Thanks this works perfectly. I guess I need to learn how to use JSON properly

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.