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:
- @model GbngWebClient.Models.BlogPublishedSelectionCriteriaVM
- <h2 class="page-header">Blog Selection Criteria</h2>
- @{
- Layout = "~/Views/Shared/_LayoutUser.cshtml";
- }
- @if (ViewBag.errormessage != null)
- {
- <p class="alert alert-danger" id="errorMessage">@ViewBag.errormessage</p>
- }
- @using (Html.BeginForm("GetBlog", "BlogPublishedController", FormMethod.Post, new { id = "formId" }))
- {
- <div style="margin-top:10px;"></div>
- <div class="panel panel-default toppanel">
- <br />
- @Html.ValidationSummary(true, "Please fix the errors below.", new { @class = "alert alert-danger" })
- <div class="panel-body">
- <div class="row">
- @* Parent dropdown list. *@
- <div class="col-md-3">
- @Html.LabelFor(model => model.BlogCategoryId, new { @class = "manadatory" })
- @Html.DropDownListFor(m => m.BlogCategoryId, new SelectList(Model.BlogPublishedCategoryList, "BlogCategoryId", "BlogCategoryDescr", Model.BlogCategoryId), "--- Select ---", new { @class = "form-control", @id = "ddlBlogCategory" })
- @Html.ValidationMessageFor(m => m.BlogCategoryId, "", new { @class = "text-danger" })
- </div>
- </div>
- <br />
- @* Cascading Dropdown list. *@
- @* - Populated by a jQuery call to the controller once a selection from the parent dropdown list is made. *@
- <div class="row">
- <div class="col-md-3">
- @Html.LabelFor(model => model.BlogId, new { @class = "manadatory" })
- @Html.DropDownListFor(m => m.BlogId, new SelectList(Model.BlogPublishedByCategoryIdList, "BlogId", "BlogTitle", Model.BlogId), "--- Select ---", new { @class = "form-control", @id = "ddlBlog" })
- @Html.ValidationMessageFor(m => m.BlogId, "", new { @class = "text-danger" })
- </div>
- </div>
- <br />
- <div style="margin-top:10px;"></div>
- </div>
- </div>
- <div class="panel-body">
- <div class="row">
- <div class="form-group">
- <div class="col-md-3">
- @* Submit button. *@
- <input type="submit" value="Get the Selected Blog" class="btn btn-primary" />
- </div>
- </div>
- </div>
- </div>
- @Html.AntiForgeryToken()
- }
- @Scripts.Render("~/bundles/jqueryval")
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/bootstrap")
- @Styles.Render("~/Content/css")
- <script type="text/javascript">
- $(document).ready(function () {
-
- loads the cascading dropdown list.
- $('#ddlBlogCategory').change(function () {
- $.ajax({
- type: "post",
- url: "/BlogPublished/LoadCascadingDropdownBlogsPublishedByCategoryId",
- data: { blogCategoryId: $('#ddlBlogCategory').val() }
- });
- });
- });
- </script>
Here's the view model - BlogPublishedSelectionCriteriaVM:
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- namespace GbngWebClient.Models
- {
- public class BlogPublishedSelectionCriteriaVM
- {
- public BlogPublishedSelectionCriteriaVM()
- {
- this.BlogPublishedCategoryList = new List<BlogPublishedCategory>();
- this.BlogPublishedByCategoryIdList = new List<BlogPublishedByCategoryId>();
- }
-
- [Required]
- [Display(Name = "Categories of Published Blogs")]
- public int BlogCategoryId { get; set; }
-
- [Required]
- [Display(Name = "Published Blogs for the Selected Category")]
- public int BlogId { get; set; }
-
- public List<BlogPublishedCategory> BlogPublishedCategoryList { get; set; }
- public List<BlogPublishedByCategoryId> BlogPublishedByCategoryIdList { get; set; }
- }
- }
Here's the controller's action method for the parent drop down list:
- [HttpGet]
- public async Task<ActionResult> LoadDropdownBlogCategorysInBlogsPublished()
- {
- BlogPublishedSelectionCriteriaVM blogPublishedSelectionCriteriaVM = new BlogPublishedSelectionCriteriaVM();
- ArgsToPassToApi argsToPassToApi = new ArgsToPassToApi();
- try
- {
- List<BlogPublishedCategory> blogPublishedCategoryList = new List<BlogPublishedCategory>();
-
- blogPublishedCategoryList = await GetBlogCategorysInBlogsPublished(Session["UserName"].ToString());
- if (blogPublishedCategoryList.Count != 0)
- {
-
- BlogPublishedCategoryInfo blogPublishedCategoryInfo = new BlogPublishedCategoryInfo();
- blogPublishedCategoryInfo.BlogPublishedCategoryInfoList = new List<BlogPublishedCategoryInfo>();
-
- foreach (var hold in blogPublishedCategoryList)
- {
- BlogPublishedCategory blogPublishedCategory = new BlogPublishedCategory
- {
- BlogCategoryId = hold.BlogCategoryId,
- BlogCategoryDescr = hold.BlogCategoryDescr,
- };
- blogPublishedSelectionCriteriaVM.BlogPublishedCategoryList.Add(blogPublishedCategory);
-
- blogPublishedCategoryInfo.BlogPublishedCategoryInfoList.Add(new BlogPublishedCategoryInfo { BlogCategoryId = blogPublishedCategory.BlogCategoryId, BlogCategoryDescription = blogPublishedCategory.BlogCategoryDescr });
- }
-
- Session["BlogPublishedCategoryList"] = blogPublishedCategoryInfo;
- }
- }
- catch (Exception ex1)
- {
- exceptionMessage = "Server error on getting the blog categorys in blogs published list. Please contact the administrator.";
- try
- {
- Error handling....
- }
- catch (Exception ex2)
- {
- ViewBag.errormessage = "Failure in ProcessClientError. Exception error: " + ex2.Message + ". Original error: " + exceptionMessage;
- }
- }
- return View("BlogPublishedSelectionCriteria", blogPublishedSelectionCriteriaVM);
- }
Here's the controller's action method for the cascading drop down list:
- [HttpPost]
- public async Task<ActionResult> LoadCascadingDropdownBlogsPublishedByCategoryId(int blogCategoryId)
- {
- BlogPublishedSelectionCriteriaVM blogPublishedSelectionCriteriaVM = new BlogPublishedSelectionCriteriaVM();
-
-
- BlogPublishedCategoryInfo blogPublishedCategoryList = (BlogPublishedCategoryInfo)Session["BlogPublishedCategoryList"];
-
- foreach (var hold in blogPublishedCategoryList.BlogPublishedCategoryInfoList)
- {
-
- BlogPublishedCategory blogPublishedCategory = new BlogPublishedCategory
- {
- BlogCategoryId = hold.BlogCategoryId,
- BlogCategoryDescr = hold.BlogCategoryDescription,
- };
-
- blogPublishedSelectionCriteriaVM.BlogPublishedCategoryList.Add(blogPublishedCategory);
- }
- blogPublishedSelectionCriteriaVM.BlogCategoryId = blogCategoryId;
- ArgsToPassToApi argsToPassToApi = new ArgsToPassToApi();
- try
- {
-
- List<BlogPublishedByCategoryId> blogPublishedByCategoryIdList = new List<BlogPublishedByCategoryId>();
-
- blogPublishedByCategoryIdList = await GetBlogsPublishedsByCategoryId(blogCategoryId, Session["UserName"].ToString());
- if (blogPublishedByCategoryIdList.Count != 0)
- {
-
- foreach (var hold in blogPublishedByCategoryIdList)
- {
- BlogPublishedByCategoryId blogPublishedByCategoryId = new BlogPublishedByCategoryId
- {
- BlogId = hold.BlogId,
- BlogTitle = hold.BlogTitle,
- };
- blogPublishedSelectionCriteriaVM.BlogPublishedByCategoryIdList.Add(blogPublishedByCategoryId);
- }
- }
- }
- catch (Exception ex1)
- {
- exceptionMessage = "Server error on getting the blogs publisheds by category id list. Please contact the administrator.";
- try
- {
- Error handling....
- }
- catch (Exception ex2)
- {
- ViewBag.errormessage = "Failure in ProcessClientError. Exception error: " + ex2.Message + ". Original error: " + exceptionMessage;
- }
- }
-
- return View("BlogPublishedSelectionCriteria", blogPublishedSelectionCriteriaVM);
- }