您可以使用jQuery ajax在國家被選中時加載狀態。
<script type="text/javascript">
$(function(){
$("#Country").change(function(){
var states="";
var countryId=$(this).val();
$.getJSON("@Url.Action("GetStates","Home")/"+countryId,function(data){
$.each(data,function(index,item){
states+="<option value='"+item.ID+"'>"+item.StateName+"</option>";
});
$("#States").html(states);
});
});
});
</script>
假設Country
是你的國家下拉列表和States
的ID是你的國家Dropdow的ID和你的JSON格式有它接受該國的ID作爲參數的操作方法,並得到了美國
public ActionResult GetStates(int id)
{
var stateList=repo.GetStatesForCountry(id);
return Json(stateList,JsonRequestBehaviour.AllowGet);
}
假設repo.GetStatesForCountry
根據傳遞的國家/地區ID返回國家/地區列表。您可以使用您的方法調用替換該函數調用,該方法調用返回State
類對象的列表。
建議:我會盡量避免動態ViewBag/ViewData的方法來填充Dropdwon並切換到一個更強類型的方法。 Here是一個如何做到這一點的例子。
您的國家下拉功能是否正常?我看到幾個問題。 – Mohayemin 2012-08-08 13:34:13
@Mohayemin你的工作完美 – siddharth 2012-08-08 13:36:56