我在MVC视图中设置了两个下拉列表,如下所示:
@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>
表格存在:
@using (Html.BeginForm("CreateEvent", "Event", FormMethod.Post, new { role = "form", id = "EventForm", data_stationListAction = @Url.Action("StationList") }))
在视图底部调用以下脚本:
<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();
});
});
});
最后,我有一个控制器动作如下:
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看起来像这样:
public static IQueryable<StationData> Stations(){
return db.Stations.ToList().AsQueryable();
}
第一个下拉列表是Populating Fine(MarketSelect),但是一旦选择了Market,就不会填充Station List。
非常感谢任何帮助。
答案 0 :(得分:0)
.trigger()需要更改为.trigger(&#34;已选择:已更新&#34;);