2012-10-02 112 views
1

我有一個2 Dropdownlistfor html,dropdownlistfor Country和dropdownlistfor狀態。即時通訊使用MVC,我想成爲這樣的輸出,如果我改變選定的國家,所選國家的狀態將被填入下拉列表狀態。我從數據庫中獲取國家和州的數據,而在州表中有一個countryId的字段。感謝幫助。MVC dropdownlist,onchange

這裏是Html示例代碼。

  <div class="fieldBlock"> 
       <div><label>Country</label>&nbsp;*</div> 
       <div>@Html.DropDownListFor(model => model.Country.Id, Model.CountrySelectList, new { @class = "width305", @id = "ddlCountry" })</div> 
       @Html.HiddenFor(model => model.Country.Name) @Html.HiddenFor(model => model.Country.Abbreviation) 
       <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div> 
      </div> 

      <div class="fieldBlock"> 
       <div>@Html.LabelFor(model => model.Employee.StateId, "State/Province")&nbsp;*</div> 
       <div>@Html.DropDownListFor(model => model.Employee.StateId, Model.CountryStateProvinceSelectList, new { @class = "width305" })</div> 
       <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div> 
      </div> 
+1

可能的重複[使用Javascript/jQuery動態填充下拉列表](http://stackoverflow.com/questions/7253187/fill-a-drop-down-list-dynamically-using-javascript-jquery) – Rafay

+0

也許你可能會得到更多的幫助,如果你真的問過一個問題,而不是僅僅陳述一堆事情,並期待我們猜測你想要什麼。 –

回答

3

試試這個

在查看

<script type="text/javascript"> 
    $(function() { 
      $("#ddlCountry").change(function() { 
       var selectedItem = $(this).val(); 
       var ddlStates = $("#ddlState"); 
       var statesProgress = $("#states-loading-progress"); 
       statesProgress.show(); 
       $.ajax({ 
        cache: false, 
        type: "GET", 
        url: "@(Url.Action("SomeAction", "SomeController"))", 
        data: { "countryId": selectedItem}, 
        success: function (data) { 
         ddlStates.html(''); 
         $.each(data, function(id, option) { 
          ddlStates.append($('<option></option>').val(option.id).html(option.name)); 
         }); 

        }, 
        error:function (xhr, ajaxOptions, thrownError){ 
         alert('Failed to retrieve states.'); 

        } 
       }); 
      }); 
     }); 
    </script> 


<div class="fieldBlock"> 
    <div><label>Country</label>&nbsp;*</div> 
    <div>@Html.DropDownListFor(model => model.Country.Id, Model.CountrySelectList, new { @class = "width305", @id = "ddlCountry" })</div> 
    @Html.HiddenFor(model => model.Country.Name) @Html.HiddenFor(model => model.Country.Abbreviation) 
    <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div> 
</div> 

<div class="fieldBlock"> 
    <div>@Html.LabelFor(model => model.Employee.StateId, "State/Province")&nbsp;*</div> 
    <div>@Html.DropDownListFor(model => model.Employee.StateId, Model.CountryStateProvinceSelectList, new { @class = "width305", @id = "ddlState" })</div> 
    <div>@Html.ValidationMessageFor(model => model.Employee.StateId)</div> 
</div> 

在控制器

public ActionResult SomeAction(string countryId) 
{ 
    //perform your action here 
    var states = _stateProvinceService.GetStateProvincesByCountryId(country.Id).ToList(); 
    var result = (from s in states 
        select new { id = s.Id, name = s.GetLocalized(x => x.Name) } 
       ).ToList(); 

    return Json(result, JsonRequestBehavior.AllowGet); 
} 

注意:檢查是否Id被正確地綁定到下拉菜單,我已經在行動寫入的假碼得到狀態。

+0

我想你需要在答案的*控制器*部分進行編輯。動作的返回類型必須是** JsonResult **。 – barnes