前台视图代码
<%:Html.DropDownList("AwardClassMainID","请选择")%>
<%:Html.DropDownList("SubID",new List<SelectListItem> { (new SelectListItem(){Text="请选择",Value="0"})})%>
//jquery代码
$("#AwardClassMainID").change(function () {
var url ="../AwardSubClasic/GetClasicList/"; //规则是控制器/方法/参数
$.getJSON(url, { firstclassid:$("#AwardClassMainID").val() }, function (data) {
$('#SubID').html('');
$('#SubID').append("<optionvalue=0>请选择</option>")
$.each(data, function (i,item) {
$('#SubID').append($("<option></option>").val(item.SecondClassID).html(item.ClassName));
});
});
});
后台Controller里代码
//view里面加一级分类的数据
ViewBag.AwardClassMainID = new SelectList(bll.GetAll(),"ClassID", "ClassName");
<summary>
/// json返回数据-通过分类ID,查询此分类的二级分类
/// </summary>
/// <param name="id">一级分类ID</param>
/// <returns></returns>
[HttpGet]
public JsonResult GetClasicList(string firstclassid)
{
List< TrainingSecondClass > Classlist = m_BLL.GetClassDropDwonList(firstclassid);
return Json(Classlist.ToList<AwardSubClasic>(),JsonRequestBehavior.AllowGet);
}
IBLL里面加接口声明
List<TrainingSecondClass>GetClassDropDwonList(string id);
BLL里面
public List<TrainingSecondClass> GetClassDropDwonList(stringid)
{
return repository.GetClassDropDwonList(db,id).ToList<TrainingSecondClass>();
}
repository里面加实现方法
public List<TrainingSecondClass>GetClassDropDwonList(SysEntities db, string FirstClassID)
{
List<TrainingSecondClass> result =new List<TrainingSecondClass>();
var n = db.TrainingSecondClass.Where(p => p.FirstClassID ==FirstClassID).ToList();
foreach (var c in n)
{
result.Add(new TrainingSecondClass
{//取出json调用的数据
SecondClassID =c.SecondClassID,
ClassName = c.ClassName
});
}
return result;
}