0

I have two DropDownListFor. If I choose a item in the first DropDownListFor the second DropDownListFor must remove some items. So to clear things up:

DropDownListFor 1 -> has items: 'a', 'b', 'c'.

DropDownListFor 2 -> has items: '1', '2', '3'.

If in DropDownListFor1 item 'a' is selected -> DropDownListFor2 only shows the items '1' and '3'.

This is DropDownListFor 1:

@Html.ExtendedDropDownListFor(m => m.Alert.TriggerType, 
    Model.TriggersDTO.Select(t => new ExtendedSelectListItem() { 
         Text = t.Translation, 
         Value = t.Type.ToString(), 
         htmlAttributes = new { data_id = t.Id } 
    }).AsEnumerable(), null, new { id = "trigger-type", name = "trigger-type" })

This is DropDownListFor 2:

@Html.DropDownListFor(m => m.Alert.Condition, 
    new SelectList(Model.ConditionsDTO, "Type", "Translation"), 
    new { id = "condition-type", name = "condition-type" })

All items come from the database.

If I change the first DropDownListFor, the next function is called:

function doTriggerTypeSelect() {
    switch ($('#trigger-type').val()) {
        case 'a':
            $('#container-a').show();
            break;
        case 'b':
            $('#container-b').show();
            break;
        case 'c':
            $('#container-c').show();
            break;
    }
}

Is it possible to do here the functionality for my issue? If you need more code, just ask.

mcuadros
  • 4,098
  • 3
  • 37
  • 44
Jeffrey
  • 183
  • 1
  • 2
  • 14
  • `If you need more code, just ask.` Actually yes, have you tried something? – Zabavsky Dec 06 '13 at 13:27
  • you can remove it javascript. but it wont be effective or scalable. i suggest you do it in the code where you bind dropdown values – Sakthivel Dec 06 '13 at 13:27
  • I didn't tried yet, because I don't know where to start. I updated my question with more code! – Jeffrey Dec 06 '13 at 13:33
  • 1
    Here you have some tutorials of cascading dropdown: [Easiest way to create a cascade dropdown in ASP.NET MVC 3 with C#](http://stackoverflow.com/questions/5497524/easiest-way-to-create-a-cascade-dropdown-in-asp-net-mvc-3-with-c-sharp), [Cascading DropDownList in ASP.Net MVC](http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx). – Zabavsky Dec 06 '13 at 13:43

1 Answers1

0
$('#trigger-type').change(function(){

        var selectedText=$('#trigger-type option:selected').text();

        // switch ($('#trigger-type').val()) {

        switch (selectedText) {
            case 'a':
                $('#container-a').show();
                break;
            case 'b':
                $('#container-b').show();
                break;
            case 'c':
                $('#container-c').show();
                break;
        }

});
Hawk
  • 90
  • 1
  • 11