开发者

AutoPostback from Html.Listbox in ASP.NET MVC?

开发者 https://www.devze.com 2022-12-21 09:23 出处:网络
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.

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号