您可以使用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