Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an HTML DropDownListFor to post back?

Tags:

asp.net-mvc

I'm building a custom pagination mechanism and I have a dropdown for changing the page size. The page size is represented in my viewmodel. Obviously I can change the value and run the search again to get the appropriate results.

But is there a way to make the DropDownListFor post back when the selected value is changed?

like image 214
whoblitz Avatar asked Dec 01 '22 09:12

whoblitz


2 Answers

You can use HtmlATtributes passing "onchange();" Try this.

<%: Html.DropDownListFor(model => model.ProductName, listobj,
                               new { onchange = 'submit()'; }) %>

Hope this helps.

like image 123
Jonas T Avatar answered Dec 10 '22 02:12

Jonas T


I actually wrote a little javascript to startup this behavior:

$(document).ready(function () {
    $('form').find('select.auto-post').change(function () {
        $(this).parents('form').submit();
    });
});

Funny coincidence, I'm actually working on the same thing right now. However, I would suggest that you don't use the POST HTTP method for any kind of paging parameters. Use GET instead. This makes navigation easier for your users, and it makes it easier for search engines to index all pages of your content.

UPDATE:

For the above jquery to work, you would render your dropdownlist like this:

Html.DropDownListFor(expression, items, new { @class = "auto-post" })
like image 23
danludwig Avatar answered Dec 10 '22 01:12

danludwig