1)当用户选择第一个下拉菜单时,可以使用jQuery ajax获取第二个下拉菜单的项目。
假设你的Category类看起来像这样
public class Category
{
public ID { set;get;}
public string Name { set;get;}
}
视图
和下拉菜单都是这样
@Html.DropDownListFor(x => x.SelectedCategoryID,
new SelectList(Model.Categories, "ID", "Name"), "Select")
@Html.DropDownListFor(x => x.SelectedSubCategoryID,
new SelectList(Model.SubCategories, "ID", "Name"), "Select")
现在有一些JavaScript来听第一个下拉的变化事件,并获得价值,请对接受类别ID的操作方法进行ajax调用,并返回JSON
格式的子类别列表。
<script type="text/javascript">
$(function() {
$("#SelectedCategoryID").change(function() {
var self = $(this);
var items="";
$.getJSON("@Url.Action("Index", "GetSubCategories")?id="+self.val(),
function(data){
$.each(data,function(index,item){
items+="<option value='"+item.ID+"'>"+item.Name+"</option>";
});
$("#SelectedSubCategoryID").html(items);
});
});
});
</script>
现在你应该有它返回JSON
格式
public ActionResult GetSubCategories(int id)
{
List<Category> subCategoryList=new List<Category>();
//to do : fill the list of (sub) categories to the
// above list for the category id passed to this method.
return Json(subCategoryList,Json.RequestBehaviour.AllowGet);
}
2)会议应该是不错的(子)分类列表GetSubCategories
操作方法。
你有什么试过?你说你的代码不起作用,然后向我们展示你的代码,看看有什么不工作。 –