First Last

First Last

  • NA
  • 648
  • 71.5k

Cascading drop down list not showing in the view in ASP.Net MVC

Aug 6 2020 4:36 PM
The parent drop down list is showing, but NOT the cascading drop down list.
 
I have a parent drop down list and a cascading drop down list. A menu selection fires off the action method to populate the parent drop down list and returns the view. The parent drop down list is shown properly.
 
 
When a selection is made in the parent drop down list, a jQuery function is called to fire off an action method to populate the cascading drop down list based upon the selection value from the parent drop down list. The view is then returned with both the parent and cascading drop down lists. However, the parent drop down list is showing, but NOT the cascading drop down list.
 
This shows that the view model has both drop down lists populated prior to returning to the view. What's showing is the cascading drop down list so I can see that it has data.
 
 
This shows the cascading drop down list NOT populated.
 
 
 
Here's the view - BlogPublishedSelectionCriteria:
  1. @model GbngWebClient.Models.BlogPublishedSelectionCriteriaVM  
  2. <h2 class="page-header">Blog Selection Criteria</h2>  
  3. @{  
  4. Layout = "~/Views/Shared/_LayoutUser.cshtml";  
  5. }  
  6. @if (ViewBag.errormessage != null)  
  7. {  
  8. <p class="alert alert-danger" id="errorMessage">@ViewBag.errormessage</p>  
  9. }  
  10. @using (Html.BeginForm("GetBlog""BlogPublishedController", FormMethod.Post, new { id = "formId" }))  
  11. {  
  12. <div style="margin-top:10px;"></div>  
  13. <div class="panel panel-default toppanel">  
  14. <br />  
  15. @Html.ValidationSummary(true"Please fix the errors below."new { @class = "alert alert-danger" })  
  16. <div class="panel-body">  
  17. <div class="row">  
  18. @* Parent dropdown list. *@  
  19. <div class="col-md-3">  
  20. @Html.LabelFor(model => model.BlogCategoryId, new { @class = "manadatory" })  
  21. @Html.DropDownListFor(m => m.BlogCategoryId, new SelectList(Model.BlogPublishedCategoryList, "BlogCategoryId""BlogCategoryDescr", Model.BlogCategoryId), "--- Select ---"new { @class = "form-control", @id = "ddlBlogCategory" })  
  22. @Html.ValidationMessageFor(m => m.BlogCategoryId, ""new { @class = "text-danger" })  
  23. </div>  
  24. </div>  
  25. <br />  
  26. @* Cascading Dropdown list. *@  
  27. @* - Populated by a jQuery call to the controller once a selection from the parent dropdown list is made. *@  
  28. <div class="row">  
  29. <div class="col-md-3">  
  30. @Html.LabelFor(model => model.BlogId, new { @class = "manadatory" })  
  31. @Html.DropDownListFor(m => m.BlogId, new SelectList(Model.BlogPublishedByCategoryIdList, "BlogId""BlogTitle", Model.BlogId), "--- Select ---"new { @class = "form-control", @id = "ddlBlog" })  
  32. @Html.ValidationMessageFor(m => m.BlogId, ""new { @class = "text-danger" })  
  33. </div>  
  34. </div>  
  35. <br />  
  36. <div style="margin-top:10px;"></div>  
  37. </div>  
  38. </div>  
  39. <div class="panel-body">  
  40. <div class="row">  
  41. <div class="form-group">  
  42. <div class="col-md-3">  
  43. @* Submit button. *@  
  44. <input type="submit" value="Get the Selected Blog" class="btn btn-primary" />  
  45. </div>  
  46. </div>  
  47. </div>  
  48. </div>  
  49. @Html.AntiForgeryToken()  
  50. }  
  51. @Scripts.Render("~/bundles/jqueryval")  
  52. @Scripts.Render("~/bundles/jquery")  
  53. @Scripts.Render("~/bundles/bootstrap")  
  54. @Styles.Render("~/Content/css")  
  55. <script type="text/javascript">  
  56. $(document).ready(function () {  
  57. // When a selection in the parent drop down list is made, it calls the controller's method and  
  58. loads the cascading dropdown list.  
  59. $('#ddlBlogCategory').change(function () {  
  60. $.ajax({  
  61. type: "post",  
  62. url: "/BlogPublished/LoadCascadingDropdownBlogsPublishedByCategoryId",  
  63. data: { blogCategoryId: $('#ddlBlogCategory').val() }  
  64. });  
  65. });  
  66. });  
  67. </script>  
Here's the view model - BlogPublishedSelectionCriteriaVM:
  1. using System.Collections.Generic;  
  2. using System.ComponentModel.DataAnnotations;  
  3. namespace GbngWebClient.Models  
  4. {  
  5. public class BlogPublishedSelectionCriteriaVM  
  6. {  
  7. public BlogPublishedSelectionCriteriaVM()  
  8. {  
  9. this.BlogPublishedCategoryList = new List<BlogPublishedCategory>();  
  10. this.BlogPublishedByCategoryIdList = new List<BlogPublishedByCategoryId>();  
  11. }  
  12. // Id for the main drop down list.  
  13. [Required]  
  14. [Display(Name = "Categories of Published Blogs")]  
  15. public int BlogCategoryId { getset; }  
  16. // Id for the cascading drop down list.  
  17. [Required]  
  18. [Display(Name = "Published Blogs for the Selected Category")]  
  19. public int BlogId { getset; }  
  20. // For the drop down lists.  
  21. public List<BlogPublishedCategory> BlogPublishedCategoryList { getset; }  
  22. public List<BlogPublishedByCategoryId> BlogPublishedByCategoryIdList { getset; }  
  23. }  
  24. }  
Here's the controller's action method for the parent drop down list:
  1. [HttpGet]  
  2. public async Task<ActionResult> LoadDropdownBlogCategorysInBlogsPublished()  
  3. {  
  4. BlogPublishedSelectionCriteriaVM blogPublishedSelectionCriteriaVM = new BlogPublishedSelectionCriteriaVM();  
  5. ArgsToPassToApi argsToPassToApi = new ArgsToPassToApi();  
  6. try  
  7. {  
  8. List<BlogPublishedCategory> blogPublishedCategoryList = new List<BlogPublishedCategory>();  
  9. // Get all the drop down data into a list.  
  10. blogPublishedCategoryList = await GetBlogCategorysInBlogsPublished(Session["UserName"].ToString());  
  11. if (blogPublishedCategoryList.Count != 0)  
  12. {  
  13. // For the list session variable.  
  14. BlogPublishedCategoryInfo blogPublishedCategoryInfo = new BlogPublishedCategoryInfo();  
  15. blogPublishedCategoryInfo.BlogPublishedCategoryInfoList = new List<BlogPublishedCategoryInfo>();  
  16. // Load the drop down list in the view model.  
  17. foreach (var hold in blogPublishedCategoryList)  
  18. {  
  19. BlogPublishedCategory blogPublishedCategory = new BlogPublishedCategory  
  20. {  
  21. BlogCategoryId = hold.BlogCategoryId,  
  22. BlogCategoryDescr = hold.BlogCategoryDescr,  
  23. };  
  24. blogPublishedSelectionCriteriaVM.BlogPublishedCategoryList.Add(blogPublishedCategory);  
  25. // Used to populate the list session variable - add to BlogPublishedCategoryInfoList.  
  26. blogPublishedCategoryInfo.BlogPublishedCategoryInfoList.Add(new BlogPublishedCategoryInfo { BlogCategoryId = blogPublishedCategory.BlogCategoryId, BlogCategoryDescription = blogPublishedCategory.BlogCategoryDescr });  
  27. }  
  28. // Populate the list session variable.  
  29. Session["BlogPublishedCategoryList"] = blogPublishedCategoryInfo;  
  30. }  
  31. }  
  32. catch (Exception ex1)  
  33. {  
  34. exceptionMessage = "Server error on getting the blog categorys in blogs published list. Please contact the administrator.";  
  35. try  
  36. {  
  37. Error handling....  
  38. }  
  39. catch (Exception ex2)  
  40. {  
  41. ViewBag.errormessage = "Failure in ProcessClientError. Exception error: " + ex2.Message + ". Original error: " + exceptionMessage;  
  42. }  
  43. }  
  44. return View("BlogPublishedSelectionCriteria", blogPublishedSelectionCriteriaVM);  
  45. }  
Here's the controller's action method for the cascading drop down list:
  1. [HttpPost]  
  2. public async Task<ActionResult> LoadCascadingDropdownBlogsPublishedByCategoryId(int blogCategoryId)  
  3. {  
  4. BlogPublishedSelectionCriteriaVM blogPublishedSelectionCriteriaVM = new BlogPublishedSelectionCriteriaVM();  
  5. // 1st: reload the parent dropdown list, get the previously populated list session variable.  
  6. // Cast.  
  7. BlogPublishedCategoryInfo blogPublishedCategoryList = (BlogPublishedCategoryInfo)Session["BlogPublishedCategoryList"];  
  8. // Load the drop down list in the view model.  
  9. foreach (var hold in blogPublishedCategoryList.BlogPublishedCategoryInfoList)  
  10. {  
  11. // Instantiate a new BlogPublishedCategory.  
  12. BlogPublishedCategory blogPublishedCategory = new BlogPublishedCategory  
  13. {  
  14. BlogCategoryId = hold.BlogCategoryId,  
  15. BlogCategoryDescr = hold.BlogCategoryDescription,  
  16. };  
  17. // Add to the view model's list.  
  18. blogPublishedSelectionCriteriaVM.BlogPublishedCategoryList.Add(blogPublishedCategory);  
  19. }  
  20. blogPublishedSelectionCriteriaVM.BlogCategoryId = blogCategoryId;  
  21. ArgsToPassToApi argsToPassToApi = new ArgsToPassToApi();  
  22. try  
  23. {  
  24. // 2nd: Get the cascading drop down data by 'category id' for the BlogPublishedByCategoryId List.  
  25. List<BlogPublishedByCategoryId> blogPublishedByCategoryIdList = new List<BlogPublishedByCategoryId>();  
  26. // Get all the cascading drop down data into a list. Passing in the selected 'category id'.  
  27. blogPublishedByCategoryIdList = await GetBlogsPublishedsByCategoryId(blogCategoryId, Session["UserName"].ToString());  
  28. if (blogPublishedByCategoryIdList.Count != 0)  
  29. {  
  30. // Load the cascading drop down list in the view model.  
  31. foreach (var hold in blogPublishedByCategoryIdList)  
  32. {  
  33. BlogPublishedByCategoryId blogPublishedByCategoryId = new BlogPublishedByCategoryId  
  34. {  
  35. BlogId = hold.BlogId,  
  36. BlogTitle = hold.BlogTitle,  
  37. };  
  38. blogPublishedSelectionCriteriaVM.BlogPublishedByCategoryIdList.Add(blogPublishedByCategoryId);  
  39. }  
  40. }  
  41. }  
  42. catch (Exception ex1)  
  43. {  
  44. exceptionMessage = "Server error on getting the blogs publisheds by category id list. Please contact the administrator.";  
  45. try  
  46. {  
  47. Error handling....  
  48. }  
  49. catch (Exception ex2)  
  50. {  
  51. ViewBag.errormessage = "Failure in ProcessClientError. Exception error: " + ex2.Message + ". Original error: " + exceptionMessage;  
  52. }  
  53. }  
  54. // Return the Views\BlogPublished\BlogPublishedSelectionCriteria.cshtml - with the model class.  
  55. return View("BlogPublishedSelectionCriteria", blogPublishedSelectionCriteriaVM);  
  56. }  

Answers (2)