,

How to add items list in the dropdown list using jquery – Cascading dropdown

Posted by

To add items to a dropdown list dynamically using jQuery and implement cascading dropdown functionality, you can follow these steps:

  1. HTML Markup:
   <select id="parentDropdown">
       <option value="">Select Parent Option</option>
       <!-- Parent dropdown options will be populated dynamically -->
   </select>
   <select id="childDropdown">
       <option value="">Select Child Option</option>
       <!-- Child dropdown options will be populated based on the selected parent option -->
   </select>
  1. JavaScript/jQuery:
   $(document).ready(function() {
       // Populate parent dropdown on page load
       populateParentDropdown();

       // Event handler for parent dropdown change
       $('#parentDropdown').change(function() {
           var parentId = $(this).val();
           populateChildDropdown(parentId);
       });
   });

   function populateParentDropdown() {
       // Make an AJAX request to retrieve the parent options
       $.ajax({
           url: '/YourController/GetParentOptions', // Replace with your controller and action method names
           type: 'GET',
           dataType: 'json',
           success: function(data) {
               // Clear existing options
               $('#parentDropdown').empty();

               // Add default option
               $('#parentDropdown').append($('<option>').text('Select Parent Option').val(''));

               // Add retrieved options
               $.each(data, function(index, option) {
                   $('#parentDropdown').append($('<option>').text(option.Name).val(option.Id));
               });
           },
           error: function(xhr, status, error) {
               console.log('Error occurred while retrieving parent options.');
           }
       });
   }

   function populateChildDropdown(parentId) {
       // Make an AJAX request to retrieve the child options based on the selected parent option
       $.ajax({
           url: '/YourController/GetChildOptions', // Replace with your controller and action method names
           type: 'GET',
           dataType: 'json',
           data: { parentId: parentId },
           success: function(data) {
               // Clear existing options
               $('#childDropdown').empty();

               // Add default option
               $('#childDropdown').append($('<option>').text('Select Child Option').val(''));

               // Add retrieved options
               $.each(data, function(index, option) {
                   $('#childDropdown').append($('<option>').text(option.Name).val(option.Id));
               });
           },
           error: function(xhr, status, error) {
               console.log('Error occurred while retrieving child options.');
           }
       });
   }
  1. C# (Server-side):
   public class YourController : Controller
   {
       // Other action methods...

       [HttpGet]
       public ActionResult GetParentOptions()
       {
           // Retrieve parent options from your data source
           var parentOptions = // Retrieve parent options as needed

           return Json(parentOptions, JsonRequestBehavior.AllowGet);
       }

       [HttpGet]
       public ActionResult GetChildOptions(int parentId)
       {
           // Retrieve child options based on the selected parent option from your data source
           var childOptions = // Retrieve child options as needed based on parentId

           return Json(childOptions, JsonRequestBehavior.AllowGet);
       }
   }

Make sure to replace “YourController” with the actual name of your controller and customize the code to retrieve the options for the parent and child dropdowns from your data source.

In this example, the parent dropdown is populated on page load by making an AJAX request to the server-side action method (GetParentOptions). The selected parent option triggers another AJAX request to retrieve the corresponding child options based on the selected parent option, which is handled by the server-side action method (GetChildOptions).

The retrieved options are

then dynamically added to the respective dropdowns using jQuery. The child dropdown is cleared and populated based on the selected parent option whenever it changes.

Remember to handle any exceptions, perform necessary validation, and adjust the code based on your specific requirements and data retrieval logic.