Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I have 2 Tables in my database Animal type and Animal Sub Type.I need to Bind my Animal sub type based on the Drop Down selected for Animal Type.

Eg:->if i select Animal Type as Birds,My Animal Subtype drop down should populate(Parrots,Cage Birds,WildBrds etc.
Posted

1 solution

You can use the jquery for this in MVC. Like as follows.

Add two drop-downs and add id html attributes to them. Add click event to each drop down option of animal type drop-down using jquery.

PHP
$('#animalDD').find('option').on('click', function () {

            $.get('@Url.Action("GetDropDownListPerSelctedItem","Home")', function(data) {

                $('#subType').html(data);

            });
        });



The $.Get will call action method and will pass that select item value as string. Then in action method you can build List<selecteditemlist> as per the passed value.

After that call one partial view, which gives you only view result for this items as follows.

@if(list!=null && list.Length>0)
{

foreach (var selectListItem in list)
{
@selectListItem.Text
}

}


As you checked, in jquery clicked event we are calling service method which gives us html 'option' result. This result then will bind to your second drop down.

So after clicked any items in one drop down, we are showing /binding respected values in other drop down.


--SDK
 
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