2011-12-13 50 views
0

之間的依賴我有ASCX文件2選擇字段:的DropDownList

<%=Html.DropDownList("CityID", new SelectList(Model.Cities, "Code", "Name"), new Dictionary<string, object> 
        { 
         {"class", "styled"}                    
        }) 
       %> 



<%=Html.DropDownList("EstablishmentID", new SelectList(Model.Establishments, "OID", "Name"), 
            "All Establishment", new Dictionary<string, object> 
        { 
         {"class", "styled"}                   
        }) 
       %> 

我想這EstablishmentId變化的值時,用戶在選擇CityID新的價值。
我該怎麼做?
謝謝。

PS。 ViewEngine是WepPages

回答

1
cascading Country and state DDL 
    @Html.DropDownListFor(model => model.CountryId, Model.CountryList, "--Select Country--", new { @class = "CountryList", style = "width:150px" }) 

    @Html.DropDownListFor(model => model.StateId, Model.StateList, "--Select State--", new { @class = "StateList", style = "width:150px" }) 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      $.post("/Client/GetModels", { id: $(".CountryList").val() }, function (data) { 
       populateDropdown($(".StateList"), data); 
      }); 
      $(".CountryList").change(function() { 
       $.post("/Client/GetModels", { id: $(this).val() }, function (data) { 
        populateDropdown($(".StateList"), data); 
       }); 
      }); 
     }); 

     function populateDropdown(select, data) { 
      $(".StateList").empty(); 
      $.each(data, function (id, option) { 
       $(".StateList").append("<option value='" + option.StateId + "'>" + option.State + "</option>"); 
      }); 
     } 
    </script> 
+0

只是改變Razor視圖來ASPX –

+0

- 斯里納 - 你可以給我一個完整的工作樣本? rick.anderson [at] Microsoft.com – RickAndMSFT

+0

嗨它工作正常瑞克 –

1
 Try this 
     In view 
      <div class="popup_textbox_div" style="padding: 0pt;"> 
      <%=Html.DropDownList("CityId", new SelectList(Model.Cities, "Code", "Name", 0), "---Select City---", new { @class = "popup_textbox popup_dropdown valid" })%> 
      </div> 

       <div class="popup_textbox_div" style="padding: 0pt;"> 
       <select class="popup_textbox popup_dropdown valid" id="OID" name="Name"> 
       <option>---All Establishment---</option> 
      </select> 
      </div> 
    Add the script 

    <script type="text/javascript"> 
    $(function() { 
     $("#CityId").change(function() { 
      var cityId = $("#CityId").val(); 

      if (!isNaN(cityId) && (cityId > 0) && (cityId != null)) { 

       GetEstablishmentsByAjax(cityId); 
      } 
      else { 
       $("#OID").html("<option value=''>---All Establishment---</option>"); 

      } 
     }); 
    }); 

    GetEstablishmentsByAjax(cid) { 

     $.ajax({ 
      type: "GET", 
      contentType: "application/json; charset=utf-8", 
      url: "/trail/selectestablishment/" + cid.toString(), 
      data: "", 
      dataType: "json", 
      success: function (data) { 

       if (data.length > 0) { 

        var options = "<option value=''> --All Establishment---</option>"; 
        for (s in data) { 
         var type = data[s]; 
         options += "<option value='" + type.Value + "'>" + type.Text + "</option>"; 
        } 
        $("#OID").html(options); 

       } 

      } 
     }); 
    } 
    </script> 

In TrailController 

public ActionResult selectestablishment(int? id) 
     { 
       SelectList establishments = null; 
       var estb = //put ur code and get list of establishments under selected city 
       establishments = new SelectList(estb, "OID", "Name"); 
       return Json(establishments, JsonRequestBehavior.AllowGet); 

     } 
+0

嗨Rick-試試這個 –