santa029
santa029

Reputation: 339

DropDownListFor automatic postback

How would I implement something like automatic postback for DropDownListFor in MVC. Currently, after selecting a value in dropdown I have to refresh the page to see the changes applied to the page.

In View,

The dropdownlistfor is like

@Html.DropDownListFor(m => m.SelectedItem, Model.MyItemList, new { @id = "DropDownListForId"})

and the onchange event is handled as such

<script type = "text/javascript">
$(function () {
    $('#DropDownListForId').change(function () {
        var item = $(this).val();
        $.ajax({
            url: '@Url.Action("SomeAction", "SomeController")',
            type: 'GET',
            data: { value: item },
            success: function(result) {

            }
        });
    });
});
</script>

Thanks!

Upvotes: 0

Views: 1166

Answers (1)

Satpal
Satpal

Reputation: 133403

I think you can simply achieve this by submitting form on change event of DropDownList

Assuming myForm as you form id

<script type = "text/javascript">
$(function () {
    $('#DropDownListForId').change(function () {
        $('form#myForm').submit();
    });
});
</script>

Upvotes: 1

Related Questions