views:

535

answers:

3

Now, i may very well just be being incredibly thick, but I'm struggling to find how to do autopostback with a Html.Listbox in ASP.NET MVC 1.

What I'm trying to achieve is just a simple if value of ListBox1 is x then the values in ListBox2 are y, if I change the value in ListBox1 to z then I want the values of ListBox2 to change based off of that information.

The information will be pulled from a Database.

I know this is easy to in standard ASP.NET, but I can't see an obvious way to do it with MVC.

Could someone point me in the right direction?

Thanks in advance for any help.

A: 

In MVC you don't have the same postback model as you do in classic ASP.NET. To do what you want, the best solution is to use javascript and add it to the onchange-attribute on the listbox and something like jquery.ajax to do a request to the server. You can also have the javascript do a post on the form and then return the full page again.

gautema
+3  A: 

postback and asp mvc are not really compatable, you should look at using jquery, very roughly like...

$(function() {
    $('#box1').change(function() {
            $.post('/controller/actionThatReturnsAPartialView',
                   { selectedID : $('box1').val()},
                   function(data){
                         $('#box2').html(data);
                   }
            );
     });
 });

MVC, by design cuts out most of the asp.net abstractions like viewstate and postback, it is much more low level.

Paul Creasey
Problem is, I'm not after returning a partial view, I just want to change the data in the second box.
Liam
data in the second box is html, which should be rendered using a partial view i would imagine, you can return json/xml and render it client side, or you can return a string if you like, it's up to you.
Paul Creasey
A: 

Based on the answers given, I eventually came up with this solution: http://blog.noma.li/2010/03/autopostback-and-cascading-drop-downs-in-asp-net-mvc-and-jquery/

Liam