Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139863

How to correctly populate a drop down list via ajax and MVC controller

$
0
0

I've searched quite a bit for this answer and can't find much that covers what I need.

I have some data stored in a db table I want to populate certain drop down lists with. On the document.ready I have an AJAX call to the controller requesting the data based on a parameter I send it. The controller returns the data as Json. I'm new to the process of Json so, figuring out what to with it once it returns is where I'm stuck.

I'm able display the data returned from the controller in an alert or console.log when it returns, so I know the right values are there, but I can't figure out how to populate the dropdown list with those values. All the data is, is about 5 to 10 ints. (not returned as ints, I know, but they're things like 65, 70, 2, 10, 11) I've tried some various options and nothing seems to work.

I can static the values in an array and that actually will populate the drop down list. I've tried populating that same array with the returned data, but no success that way. Here is the ajax call:

//Fill symbols drop down list
function returnSymbols(cc) {

var sendData = JSON.stringify({ 'ul': cc });
$.ajax({
    url: '/Trucking/returnSymbols',
    type: 'POST',
    contentType: 'application/json',
    data: sendData,
    success: function (data) {
        //alert('success');
        console.log('success, yes');
        alert(data);
        var numbers = [];
        var obj = jQuery.parseJSON(data);
       /* If I do this and static these, it does work 
          var numbers = [1, 2, 3, 4, 5]  */
        var option = '';
        for (var i = 0; i < numbers.length; i++) {
            option += '<option value="' + numbers[i] + '">' + numbers[i] + '</option>';
        }
        $('#ddlMcalSymbols').append(option); //fill ddl with these values. 

    },
    error: function () {
        //alert('Error');
        console.log('Error');
    }
});

}

To reiterate I have tried things like numbers.push(obj) or even. .push(data), but those aren't working. Since the controller returns a Json value I was under the impression I needed to parse that Json in order to do anything with it. Here is the controller if it helps at all:

    [HttpPost]
    public ActionResult returnSymbols(string ul)
    {
        List<Get_CIF_SymbolsVM> symbols;

        Guid newGuid = Guid.Parse(ul); //parse param as Guid

        using (TruckingDb db = new TruckingDb())
        {
            symbols = db.GetSymbols.ToArray().OrderBy(x => x.RID).Select(x => new Get_CIF_SymbolsVM(x)).ToList();

        }
        var syms = (from s in symbols
                    where s.UniqLineType == newGuid
                    select s.SymbolCode).Distinct();

        return Json(syms, JsonRequestBehavior.AllowGet);
    }

Any help would be greatly appreciated.


Viewing all articles
Browse latest Browse all 139863

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>