Triet Doan
Triet Doan

Reputation: 12085

Trigger event in MVC Framework

As you've known, it's very easy to trigger an event in WebForm, but it's a problem in MVC Framework. For example, I have 2 DropDownList, Country and State. I want to load the data in State base on selected Country. In WebForm, I can trigger the SelectedIndexChange event, but in MVC Framework, what should I do?

Please help. Thanks in advance.

Upvotes: 2

Views: 2190

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

In WebForm, I can trigger the SelectedIndexChange event, but in MVC Framework, what should I do?

You could use javascript and subscribe to the onchange javascript event of the dropdown.

For example if you use jQuery:

<script type="text/javascript">
    $(function() {
        $('#id_of_your_drop_down').on('change', function() {
            // the value of the dropdown changed. Here you could do whatever
            // you intended to do. For example you could send the selected value
            // to a controller action using an AJAX call.
            var selectedValue = $(this).val();
            var url = '@Url.Action("SomeAction")';
            $.post(url, { value: selectedValue }, function(result) {
                // The AJAX request completed successfully. Here you could
                // do something with the results returned by the server
            });
        });
    });
</script>

Upvotes: 5

Related Questions