简体   繁体   中英

Cascading DropDownList is not Populating MVC

I have two drop down lists setup in an MVC view as follows:

@Html.DropDownListFor(model => model.Market, 
    new SelectList(ListInfo.Markets(), "Name", "Name"), "Markets..."
    , new { @class = "form-control", @id = "MarketSelect" })

<select id="StationSelect" name="SelectedStations" class="chosen-select form-control" multiple></select>

The form beings with:

@using (Html.BeginForm("CreateEvent", "Event", FormMethod.Post, new { role =   "form", id = "EventForm", data_stationListAction = @Url.Action("StationList") }))

The following script is called at the bottom of the view with:

<script src="@Url.Content("~/Scripts/marketStation.js")"></script>

$(function () {
$('#MarketSelect').change(function () {
    var URL = $('#EventForm').data('stationListAction');
    $.getJSON(URL + '/' + $('#MarketSelect').val(), function (data) {
        var items = '<option>Stations</option>';
        $.each(data, function (i, station) {
            items += "<option value='" + station.Value + "'>" + station.Text + "</option>";
        });
            $('#StationSelect').html(items);
            $("#StationSelect").trigger("liszt:updated");
            $("#StationSelect").change();
        });
    });
});

Finally, I have a Controller Action as follows:

public ActionResult StationList(string market) {
        string Market = market;

        var stations = from s in ListInfo.Stations()
                       where s.MarketName == Market
                       select s;

        if(HttpContext.Request.IsAjaxRequest())
        {
            return Json(new MultiSelectList(
                stations.ToArray(),
                "SalemOrgObjID",
                "Name"),
                JsonRequestBehavior.AllowGet);
        }
        return RedirectToAction("CreateEvent");
    }

ListInfo.Stations looks like this:

public static IQueryable<StationData> Stations(){
    return db.Stations.ToList().AsQueryable();
}

The first Drop Down List is Populating Fine (MarketSelect), but once a Market is selected, the Station List is not populated.

Any help is greatly appreciated.

.trigger()需要更改为.trigger(“ chosen:updated”);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM