Jeremy Foster
Jeremy Foster

Reputation: 4763

jQuery Select Change Does a "Postback"

I should know how to do this one, but I'm at the point where I think it would be more efficient to just ask.

I have a select field in the _Layout.cshtml in an MVC3 project. When the user selects a new value from the field, I want to store their selection in a session variable and then refresh the page with changes based on that session variable (much like a postback in the forms model). What would be the best way to do this.

Thanks.

Upvotes: 0

Views: 675

Answers (1)

alexn
alexn

Reputation: 58982

You need to send the data to your server if you need to store it in a session variable. A ajax call would be your best bet here. When you get a response back from server, redirect to the same page. Something like this:

$('select').change(function() {
    $.get('/Url/To/Controller/Action', function(responseData) {
        document.location = document.location; // Redirect to self
    });
});

Upvotes: 2

Related Questions