Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I am trying to make an cascading dropdown function with partial view. The first dropdown I have two categories, and when I click on the category it finds the children for that category and fill the second dropdown list ( on my partial view ).

When I click on the second dropdown menu I will find the children for that category I click on, and then a new dropdown will populate with this new information.

Can someone help me in the right direction?

This is my controller :

C#
[HttpGet]
        public PartialViewResult CategoryList(int catId)
        {

            var subcategories = (from s in db.Categories
                                 where s.ParentCategoryId == catId
                                 select s).ToList();
            ViewBag.CategorySub = new SelectList(subcategories, "CategoryId", "Name");
            
            return PartialView("DropDownList", subcategories);

        }
        [HttpGet]
        public PartialViewResult SubCategoryList(int idSub)
        {

            var subcategories = (from s in db.Categories
                                 where s.ParentCategoryId == idSub
                                 select s).ToList();
            ViewBag.CategorySub = new SelectList(subcategories, "CategoryId", "Name");

            return PartialView("DropDownList", subcategories);

        }


The script on the view :

JavaScript
//Run Jquery script
   jQuery(document).ready(function ($) {
       //Dropdownlist Selectedchange event

       $("#CategoryId").change(function () {
           //Take the Id from the dropdown list when selected an item
           var id = $(this).val();
           //Make the dropdown empty

           //Send the information to the controller

           $.ajax({
               type: 'GET',
               url: '@Url.Action("CategoryList", "Manage")', // we are calling json method

               data: { catId: id },
               dataType: 'html',
               contentType: 'application/html; charset-utf-8',
               //here we are get value of selected item and passing same value
               // to input to the json method CategoryList.
               success: function (data) {
                   $('#dynamicContent').show();
                   $("#dynamicContent").html(data);
               }
           });
       });
       $('body').on("change", "#CategorySub", function (evt) {
           var idSub = $(this).val();

           alert(idSub);
           $.ajax({
               type: 'GET',
               url: '@Url.Action("CategoryList", "Manage")', // we are calling json method
               context: document.body,
               data: { idSub: ids },
               dataType: 'html',
               contentType: 'application/html; charset-utf-8',
               //here we are get value of selected item and passing same value
               // to input to the json method CategoryList.
               success: function () {
                   alert('done');
               }
           });


       });
});
Posted

1 solution

Please share HTML of your view. First of All you need 3 different dropdown that hopefullly will be there as you did not post your HTML.

PartialViews has to reloaded with javascript and to achieve this partialview should be in some container like
HTML
<div id="partialview">@{Html.RenderPartial("yourPartialView",modelobject,viewdatadictionary)}</div>


then in javascript you have to reload that container div of your partialview.

$("#partialview").load("@Html.Action("ActionName","ControllerName",querystring values)");


in your case:
$("#CategoryId").change(function () {
           //Take the Id from the dropdown list when selected an item
           var id = $(this).val();
$("#partialview").load("@Html.Action("CategoryList","Manage",new {catId=id})");


your partial view will be reloaded acording to your need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900