Question

I know MVC doesn't have autoposback functionality and that needs to be done using JS/JQuery, and that is when my problems start... don't know how to do it yet.

This is how I populate my ddl:

@Html.DropDownListFor(x => x.CurrentCountry,
                  new SelectList(Model.Countries.ToList(), "Code", "Name"))

and my URL have this format:

localhost/Products/?country=US&currency=USD&category=17&page=2

How do I add postback functionality to take the selected country?

Thanks.

Was it helpful?

Solution

I know MVC doesn't have autoposback functionality and that needs to be done using JS/JQuery

Not necessary. You could put the dropdown in an HTML <form> and when the user submits this form the selected value will be sent to the server.

If on the other hand you want to transmit the selected value when the user changes selection then you need to use javascript and subscribe to the onchange event. For example if you use jQuery:

$(function() {
    // subscribe to the change event of the dropdown
    $('#CurrentCountry').change(function() {
        // find the containing form and submit it
        $(this).closest('form').submit();
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top