I tried to implement the example here
https://www.c-sharpcorner.com/article/creating-simple-cascading-dropdownlist-in-asp-net-core-mvc-with-new-tag-helpers/
into my Razor page project but the second dropdown doesn't get any data. What I did is
View Page including script
- <div class="col-2">
- <label for="PTWReviewD.Category" class="form-control badge-primary">PTW Review Category</label>
- <select asp-for="PTWReviewD.Category" class="form-control border-warning" asp-items="Model.PTWRCList">
- <option value="">--Select PTW Review Category--</option>
- </select>
- <span asp-validation-for="PTWReviewD.Category" class="text-danger"></span>
- </div>
- <div class="col-2">
- <label for="PTWReviewD.SubCategory" class="form-control badge-primary">PTW Review Subcategory</label>
- <select asp-for="PTWReviewD.SubCategory" class="form-control border-warning" asp-items="@(new SelectList(string.Empty, "RSubCategoryID", "RSubCategoryS"))">
- <option value="">--Select PTW Review Subcategory--</option>
- </select>
- <span asp-validation-for="PTWReviewD.SubCategory" class="text-danger"></span>
- </div>
-
- @Html.AntiForgeryToken()
-
- @section scripts{
-
- <script>
- $(function () {
- $("#Category").on("change", function func() {
- var url = '@Url.Content("~/")' + "PTWReview/OnGetSubCategoriesAsync";
- var idSource = "#RCategoryID";
-
- $.getJSON(url, { RCategoryID: $(idSource).val() }, function (data) {
- var items = '';
- $("#SubCategory").empty();
- $.each(data, function (i, ptwrsclist) {
- items += "<option value='" + ptwrsclist.RSubCategoryID + "'>" + ptwrsclist.RSubCategoryS + "</option>";
- });
- $("#SubCategory").html(items);
- });
- });
- });
- </script>
-
- }
Getting the data with
- public async Task<IActionResult> OnGetAsync(int ptwNoId)
- {
-
-
- PTWRCList = await _context.PTWRCategories.Select(a =>
- new SelectListItem
- {
- Value = a.RCategoryID.ToString(),
- Text = a.RCategoryS
- }).ToListAsync();
-
-
- return Page();
- }
-
- public async Task<JsonResult> OnGetSubCategoriesAsync(int RCategoryID)
- {
- PTWRSCList = await _context.PTWRSubCategories.Where(a => a.RCategoryID == RCategoryID).Select(a =>
- new SelectListItem
- {
- Value = a.RSubCategoryID.ToString(),
- Text = a.RSubCategoryS
- }).ToListAsync();
-
- return new JsonResult(new SelectList(PTWRSCList, "RSubCategoryID", "RSubCategoryS"));
-
- }
The first ddl is filled with data but the second is not and I have no errors. What am I doing wrong?