通过ViewBag从控制器方法为Html.DropdownList赋值

时间:2014-06-07 18:55:21

标签: c# jquery ajax

我的视图上有一个@ Html.DropDownList,其初始值为null。必须根据其他dropDownList中的选定值填充列表的值。我有两个dropDownLists,如下所示

            <div class="col-sm-7">
                @Html.DropDownList("City", null, new {@id="City", @class="form-control"})
            </div>
            <div class="col-sm-7">
                @Html.DropDownList("Theatre", null, new {@id = "Theatre", @class = "form-control"})               
            </div>

必须根据城市dropDown列表中选择的值填充剧院。由于Theatre不能为null,我最初将ViewBag.Theatre设置为控制器动作方法中的空列表。一旦选择了一个城市,我正在从jQuery进行ajax调用,在我的控制器中调用另一个方法将ViewBag.Theatre设置为我返回的列表。

但剧院dropDown的内容仍然是空的。如何在ViewBag.atre值更改后刷新dropDown的内容?

这是我调用控制器方法的jQuery

$("#City").change(function() {
if ($("#City").val() != 0) {
    var ty = $("#City").val();
    $.ajax({
        url: rootDir + "/AnalysisHistory/SetTheatres",
        data: {city: city },
        type: "POST",
        dataType: "html",
        success: function () {
            $(".form").show();
            $("#loading").hide();
            $("#analysisDetails").slideDown('slow');


    });
} else {
    $(".form").hide();
}

});

1 个答案:

答案 0 :(得分:0)

关于城市下拉列表的更改,以下是我在电影院下拉列表中所做的工作:   •我清空了影院下拉列表的内容   •当我收到新列表作为JSON时,在成功时,我将使用新内容重新填充列表。这是我的代码

success: function (datax) {
                var result = "";
                var listObj = $.parseJSON(datax);
                $(listObj).each(function (i, val) {
                    result += '<option value="' + val.Value + '">' + val.Text + '</option>';
                });
                $("#Theatre").html(result);
                $(".form").show();
                $("#loading").hide();
                $("#analysisDetails").slideDown('slow');
            }
相关问题