none
ASP.NET MVC Dropdown: when setting selected value from controller onchange event is not firing RRS feed

  • Question

  • I have an ASP.NET MVC app with 2 dropdowns, where DD#2 is populated based on the selection in DD#1.  The DDs are being populated with Lists from a database, and with jquery I have the onchange events functioning and the app works great.

    I am now attempting to set DD#1 using an incoming QueryString (instead of selecting an item from the DD).  I am specifying the selectedValue option in the SelectList call.  This works great, and DD#1 is being set to the incoming QueryStrng value, but the onchange event is not firing so DD#2 is not being populated. 

    Code snippets

    // DD#1 onchange event
    // Fires as expected when selecting an item from the dropdown
    $(document).ready(function () {
            //  StationId Dropdown change function
            $("#StationId").change(function () {
                alert("Got here");
                $.get("/Chart/GetParameterListByName", { StationName: $("#StationId option:selected").text() }, function (data) {
                    $("#ParameterId").empty();
                    $("#ParameterId").append("<option value='null'>--Select Parameter</option>")
                    $.each(data, function (index, row) {
                        $("#ParameterId").append("<option value='" + row.LongName + "'>" + row.LongName + "</option>")
                    })
                });
            })
    
    
    //  Controller snippet that takes an incoming QueryString and sets the selectedValue for DD#1 in the ListSelect Call
    ViewBag.StationList = new SelectList(db.Stations.Where(p => p.StationName.Contains("FR")), "StationNumber", "StationName",stationNumber);
    
     

    DD#1 item is selected correctly after this call, but the onchange event does not fire to update DD#2

    Any suggestions/ideas would be greatly appreciated.


    Warren M

    • Moved by Sabah ShariqMVP Sunday, March 11, 2018 2:35 PM Moved From Visual C#
    Sunday, March 11, 2018 6:02 AM

All replies