ddess
ddess

Reputation: 13

How to call different actions in a controller with a button from view?

I'm using Html.Beginform with a submit button in it, I'm sending my model variables to a controller and calling the action which I write in the Html.Beginform attribute. I want to do that, when I choose a dropdown select, based on this I want to call different actions.

For example when I select apple from dropdown, I want to my button to call actino method Apple, when I choose cherry, I want to my button to call action method Cherry. Is this possible? Thanks for answers.

Upvotes: 0

Views: 31

Answers (1)

Jamshaid K.
Jamshaid K.

Reputation: 4567

So, you want to call Apple when someone selects Apple from the dropdown and Cherry when someone selects Cherry from the dropdown.

Pretty simple and straight forward approach would be to use JavaScript/jQuery to dynamically change the action of the form. Bind the jquery change event for the dropdown:

$(document).ready(function() {
    $('#mySelectElementId').on('change', function() { 
        var selectedValue = $(this).val();
        if(selectedValue == 'apple'){
            $('#myform').attr('action', '/ControllerName/Apple')
        } else if(selectedValue == 'cherry'){
            $('#myform').attr('action', '/ControllerName/Cherry')
        }
    });
});

Upvotes: 1

Related Questions