C#
public class ItemController : Controller
{
DB_ITEMEntities db = new DB_ITEMEntities();
public ActionResult Index()
{
ViewBag.catVG =new SelectList( db.SP_BIND_CATEGORY(),"CATID","CATNAME");
return View();
}
public JsonResult GetSubcat(int catid)
{
var subcat = db.SP_BIND_SUBCAT_BY_CAT(catid);
return Json(new SelectList(subcat, "SUBCATID", "SUBCATNAME"));
}
}
Category
@Html.DropDownList("ddlCat", ViewBag.catVG as SelectList,new { style = "width:100px" })
SubCategory
@Html.DropDownList("ddlSubCat", new SelectList(string.Empty, "Value", "Text"), "Please select a Sub Category", new { style = "width:100px" })
$(document).ready(function () {
$("#ddlCat").change(function () {
$("#ddlSubCat").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetSubcat")',
dataType: 'json',
data: { id: $("#ddlCat").val() },
success: function (subcategories) {
$.each(subcategories, function (i, subcategory) {
$("#ddlSubCat").append('' +
subcategory.Text + '');
});
},
error: function (ex) {
alert('No Sub Categories Found : ' + ex);
}
});
return false;
})
});Only the category in the dropdown is binding, But the subcategory items are not fetched on selection of category. Tried out several combinations, but **unable to fetch the subcategory**.


Salman BegRank #284 · 8.8K reputationPosted Jun 7, 2021, 7:51 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Jun 7, 2021, 8:23 AM EDT
do it as shown below
step 2. On success
''
Savior46 reputationPosted Jun 12, 2021, 5:24 AM EDT
Jignesh KumarRank #91 · 22.9K reputationPosted Jun 12, 2021, 8:45 AM EDT
Hi,
Are you able to hit SubCategory method ?
Please refer this link which will help you,
https://www.c-sharpcorner.com/UploadFile/3d39b4/casecading-dropdown-list-with-mvc-linq-to-sql-and-ajax/
https://www.abctutorial.com/post/26/how-to-create-cascading-dropdownlist-in-aspnet-mvc--using-jquery-ajax