Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:50:08+00:00 2026-05-24T16:50:08+00:00

I am using the latest version of jQuery and ASP.NET MVC 3 with the

  • 0

I am using the latest version of jQuery and ASP.NET MVC 3 with the Razor view engine.

I have tried Google looking for a decent example of loading a child drop down list when a parent drop down item is selected. I am looking to do this via jQuery AJAX using JSON. My knowledge of this is zero.

I have a Category class with a list of categories. It’s a parent-child association.

If I select a category from the parent drop down list, then all the child categories need to be listed in the child drop down list for the selected parent category.

This is what I currently have, but need to complete it, not sure if I am in the right direction:

$(document).ready(function () {
   $('#ddlParentCategories').change(function () {
      alert('changed');
   });
});

I loaded my drop down list from my view model as such:

@Html.DropDownListFor(x => x.ParentCategoryId, new SelectList(Model.ParentCategories, "Id", "Name", Model.ParentCategoryId), "-- Select --", new { id = "ddlParentCategories" })

The first item has text “– Select –” (for both parent and child drop down lists). On initial page load nothing must be loaded in the child drop down list. When a value is selected then the child drop down list must be populated. And when “– Select –” is selected again in the parent drop down list then all the items in the child drop down list must cleared except “– Select –“.

If possible, if the child categories is loading, how do I display that “round” loading icon?

UPDATE

I have updated my code to Darin’s code, and I cannot get it to work properly:

Category class:

public class Category
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string Description { get; set; }
   public string MetaKeywords { get; set; }
   public string MetaDescription { get; set; }
   public bool IsActive { get; set; }
   public int? ParentCategoryId { get; set; }
   public virtual Category ParentCategory { get; set; }
   public virtual ICollection<Category> ChildCategories { get; set; }
}

EditProductViewModel class:

public class EditProductViewModel
{
   public int Id { get; set; }
   public string Name { get; set; }
   public string ShortDescription { get; set; }
   public string LongDescription { get; set; }
   public bool IsActive { get; set; }
   public string PageTitle { get; set; }
   public bool OverridePageTitle { get; set; }
   public string MetaKeywords { get; set; }
   public string MetaDescription { get; set; }
   public int ParentCategoryId { get; set; }
   public IEnumerable<Category> ParentCategories { get; set; }
   public int ChildCategoryId { get; set; }
   public IEnumerable<Category> ChildCategories { get; set; }
}

ProductController class:

public ActionResult Create()
{
   EditProductViewModel viewModel = new EditProductViewModel
   {
      ParentCategories = categoryService.GetParentCategories()
         .Where(x => x.IsActive)
         .OrderBy(x => x.Name),
      ChildCategories = Enumerable.Empty<Category>(),
      IsActive = true
   };

   return View(viewModel);
}

public ActionResult AjaxBindingChildCategories(int parentCategoryId)
{
   IEnumerable<Category> childCategoryList = categoryService.GetChildCategoriesByParentCategoryId(parentCategoryId);

   return Json(childCategoryList, JsonRequestBehavior.AllowGet);
}

Create view:

<tr>
   <td><label>Parent Category:</label> <span class="red">*</span></td>
   <td>@Html.DropDownListFor(x => x.ParentCategoryId,
         new SelectList(Model.ParentCategories, "Id", "Name", Model.ParentCategoryId),
         "-- Select --",
         new { data_url = Url.Action("AjaxBindingChildCategories"), id = "ParentCategories" }
      )
      @Html.ValidationMessageFor(x => x.ParentCategoryId)
   </td>
</tr>
<tr>
   <td><label>Child Category:</label> <span class="red">*</span></td>
   <td>@Html.DropDownListFor(x => x.ChildCategoryId,
         new SelectList(Model.ChildCategories, "Id", "Name", Model.ChildCategoryId),
         "-- Select --",
         new { id = "ChildCategories" }
      )
      @Html.ValidationMessageFor(x => x.ChildCategoryId)
   </td>
</tr>

<script type="text/javascript">

   $(document).ready(function () {
      $('#ParentCategories').change(function () {
         var url = $(this).data('url');
         var data = { parentCategoryId: $(this).val() };

         $.getJSON(url, data, function (childCategories) {
            var childCategoriesDdl = $('#ChildCategories');
            childCategoriesDdl.empty();

            $.each(childCategories, function (index, childCategory) {
               childCategoriesDdl.append($('<option/>', {
                  value: childCategory, text: childCategory
               }));
            });
         });
      });
   });


</script>

It goes into my AjaxBindingChildCategories action and it brings back records, it just doesn’t want to display my child category dropdownlist. I had a look in Fire Bug and the error that I get is:

GET AjaxBindingChildCategories?parentCategoryId=1

500 Internal Server Error
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:50:09+00:00Added an answer on May 24, 2026 at 4:50 pm

    Here’s an example of cascading drop down lists. As always start by defining a view model:

    public class MyViewModel
    {
        [DisplayName("Country")]
        [Required]
        public string CountryCode { get; set; }
        public IEnumerable<SelectListItem> Countries { get; set; }
    
        public string City { get; set; }
        public IEnumerable<SelectListItem> Cities { get; set; }
    }
    

    then a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                // TODO: Fetch countries from somewhere
                Countries = new[]
                {
                    new SelectListItem { Value = "FR", Text = "France" },
                    new SelectListItem { Value = "US", Text = "USA" },
                },
    
                // initially we set the cities ddl to empty
                Cities = Enumerable.Empty<SelectListItem>()
            };
            return View(model);
        }
    
        public ActionResult Cities(string countryCode)
        {
            // TODO: based on the selected country return the cities:
            var cities = new[]
            {
                "Paris", "Marseille", "Lyon"
            };
            return Json(cities, JsonRequestBehavior.AllowGet);
        }
    }
    

    a view:

    @model MyViewModel
    
    @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(x => x.CountryCode)
            @Html.DropDownListFor(
                x => x.CountryCode, 
                Model.Countries, 
                "-- Select country --", 
                new { data_url = Url.Action("cities") }
            )
            @Html.ValidationMessageFor(x => x.CountryCode)
        </div>
    
        <div>
            @Html.LabelFor(x => x.City)
            @Html.DropDownListFor(
                x => x.City, 
                Model.Cities, 
                "-- Select city --"
            )
            @Html.ValidationMessageFor(x => x.City)
        </div>
    
        <p><input type="submit" value="OK" /></p>
    }
    

    and finally the unobtrusive javascript in a separate file:

    $(function () {
        $('#CountryCode').change(function () {
            var url = $(this).data('url'); 
            var data = { countryCode: $(this).val() };
            $.getJSON(url, data, function (cities) {
                var citiesDdl = $('#City');
                citiesDdl.empty();
                $.each(cities, function (index, city) {
                    citiesDdl.append($('<option/>', {
                        value: city,
                        text: city
                    }));
                });
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET MVC 3 with razor and the latest version of the
I am using the latest version of Telerik MVC with my ASP.NET MVC 3
I am using the latest version of protobuf-net with VS2008 integration. I have created
I am using autoNumeric plugin with the latest version of jQuery . I have
I have an ASP.NET MVC application that has a jQuery Treeview and a jQuery
i have an html table inside a form in an asp.net mvc view. I
I am using latest version of jQuery Autocompletion plugin and have populated an array
I'm using asp.net mvc 3 + jquery with plugins such as jqgrid. And the
I have the following web service created and using the latest version of jquery
I'm using the latest version (v1.6.2) of nyromodal lightbox : jQuery.nyroModalSettings({title:'Manual Title'}); It won't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.