Skip to content
Loading
Cascading drop down list not showing in the view in ASP.Net MVC  
  •   
  •   
  • "margin-top:10px;">  
  •   
  •   
  • class="panel-body">  
  • class="row">  
  • class="form-group">  
  • class="col-md-3">  
  • @* Submit button. *@  
  • "submit" value="Get the Selected Blog" class="btn btn-primary" />  
  •   
  •   
  •   
  •   
  • @Html.AntiForgeryToken()  
  • }  
  • @Scripts.Render("~/bundles/jqueryval")  
  • @Scripts.Render("~/bundles/jquery")  
  • @Scripts.Render("~/bundles/bootstrap")  
  • @Styles.Render("~/Content/css")  
  • "text/javascript">  
  • $(document).ready(function () {  
  • // When a selection in the parent drop down list is made, it calls the controller's method and  
  • loads the cascading dropdown list.  
  • $('#ddlBlogCategory').change(function () {  
  • $.ajax({  
  • type: "post",  
  • url: "/BlogPublished/LoadCascadingDropdownBlogsPublishedByCategoryId",  
  • data: { blogCategoryId: $('#ddlBlogCategory').val() }  
  • });  
  • });  
  • });  
  •   
  • 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();  
    10. this.BlogPublishedByCategoryIdList = new List();  
    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 BlogPublishedCategoryList { getset; }  
    22. public List BlogPublishedByCategoryIdList { getset; }  
    23. }  
    24. }  
    Here's the controller's action method for the parent drop down list:
    1. [HttpGet]  
    2. public async Task LoadDropdownBlogCategorysInBlogsPublished()  
    3. {  
    4. BlogPublishedSelectionCriteriaVM blogPublishedSelectionCriteriaVM = new BlogPublishedSelectionCriteriaVM();  
    5. ArgsToPassToApi argsToPassToApi = new ArgsToPassToApi();  
    6. try  
    7. {  
    8. List blogPublishedCategoryList = new List();  
    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();  
    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 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 blogPublishedByCategoryIdList = new List();  
    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. }  

    2 Replies

    Know the answer? Post it — somebody with the same question will find it here.