Front-end view code
<%: Html. DropDownList ("AwardClassMainID", "select") %>
<%: Html. dropDownList ("SubID", new List <SelectListItem >{( new SelectListItem () {Text = "select", Value = "0"}) %>
// Jquery code
$ ("# AwardClassMainID"). change (function (){
Var url = "../AwardSubClasic/GetClasicList/"; // The rule is Controller/method/Parameter
$. GetJSON (url, {firstclassid: $ ("# AwardClassMainID"). val ()}, function (data ){
Certificate ('{subid'}.html ('');
$ ('# Subid'). append ("<optionvalue = 0> select </option> ")
$. Each (data, function (I, item ){
$ ('# Subid'). append ($ ("<option> </option>" pai.val(item.secondclassid#.html (item. ClassName ));
});
});
});
Code in the Controller in the background
// Add level-1 classified data to the view
ViewBag. AwardClassMainID = new SelectList (bll. GetAll (), "ClassID", "ClassName ");
<Summary>
/// Json return data-query the second-level category of this category by category ID
/// </Summary>
/// <Param name = "id"> Level 1 category ID </param>
/// <Returns> </returns>
[HttpGet]
Public JsonResult GetClasicList (string firstclassid)
{
List <TrainingSecondClass> Classlist = m_BLL.GetClassDropDwonList (firstclassid );
Return Json (Classlist. ToList <AwardSubClasic> (), JsonRequestBehavior. AllowGet );
}
Add interface declaration in IBLL
List <TrainingSecondClass> GetClassDropDwonList (string id );
BLL
Public List <TrainingSecondClass> GetClassDropDwonList (stringid)
{
Return repository. GetClassDropDwonList (db, id). ToList <TrainingSecondClass> ();
}
Add implementation methods in 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
{// Retrieve the json call data
SecondClassID = c. SecondClassID,
ClassName = c. ClassName
});
}
Return result;
}