Skip to content
Loading
  
  •   
  • I see the right number of items in the second dropdownlist but text is "undefined". 
    0
  • Marius Vasile
    0
    Got it finally
     
    1. "post">  
    2.     class="col-2">  
    3.         for="PTWReviewD.Category" class="form-control badge-primary">PTW Review Category  
    4.         "CategoryID" asp-for="PTWReviewD.Category" class="form-control border-warning" asp-items="Model.PTWRCList">  
    5.             "">--Select PTW Review Category--  
    6.           
    7.         for="PTWReviewD.Category" class="text-danger">  
    8.     
      
  •     class="col-2">  
  •         for="PTWReviewD.SubCategory" class="form-control badge-primary">PTW Review Subcategory  
  •         "SubCategoryID" asp-for="PTWReviewD.SubCategory" asp-items="@(new SelectList(string.Empty, "RSubCategoryID", "RSubCategoryS"))" class="form-control border-warning">  
  •             "">--Select PTW Review Subcategory--  
  •           
  •         for="PTWReviewD.SubCategory" class="text-danger">  
  •       
  •   
  •  
     and script
    1. $(function () {  
    2.         $("#CategoryID").on("change", function () {  
    3.             var categoryId = $(this).val();  
    4.             $("#SubCategoryID").empty();  
    5.             $("#SubCategoryID").append("--Select PTW Review Subcategory--");  
    6.             $.getJSON(`?handler=SubCategories&id=${categoryId}`, (data) => {  
    7.                 $.each(data, function (i, item) {  
    8.                     $("#SubCategoryID").append(`"${item.value}"> ${item.text}`);  
    9.                 });  
    10.             });  
    11.         });  
    12.     });  
     and Json
     
    1. public async Task OnGetSubCategoriesAsync(int id)  
    2.         {  
    3.             var ptwrscList = await (from a in _context.PTWRSubCategories where a.RCategoryID==id  
    4.                                     select a).ToListAsync();  
    5.   
    6.             return new JsonResult(new SelectList(ptwrscList, "RSubCategoryID", "RSubCategoryS"));  
    7.         }  
     
    0