2014-06-07 25 views
0

我的視圖上有@ Html.DropDownList,它的初始值爲null。列表的值必須基於從其他dropDownList中選擇的值填充。我有我的兩個dropDownLists如下通過ViewBag從控制器方法爲Html.DropdownList分配值

  <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列表中選擇的值填充。由於劇院不能爲空,我最初將ViewBag.Theatre設置爲我的控制器的操作方法中的一個空列表。一旦選擇了一個城市,我正在做一個來自jQuery的ajax調用,以在我的控制器中調用另一種方法來將ViewBag.Theatre設置爲我的返回列表。

但Theatre dropDown的內容仍爲空。如何在ViewBag.Theatre值更改後刷新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(); 
} 

});

回答

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'); 
      } 
相關問題