2012-04-19 56 views
0

我知道MVC没有autoposback功能,需要使用JS/JQuery来完成,那就是当我的问题开始时......不知道该怎么做。MVC3 DDL Autopostback选择像

这是我如何填充我的DDL:

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

和我的URL具有以下格式:

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

如何添加回传功能,把选定的国家?

谢谢。

回答

0

我知道MVC没有autoposback的功能和需要使用JS/JQuery的

没必要做。您可以将下拉列表放在HTML <form>中,当用户提交此表单时,所选值将发送到服务器。

如果您希望在用户更改选择时传输选定值,则需要使用javascript并订阅onchange事件。例如,如果你使用jQuery:

$(function() { 
    // subscribe to the change event of the dropdown 
    $('#CurrentCountry').change(function() { 
     // find the containing form and submit it 
     $(this).closest('form').submit(); 
    }); 
}); 
+0

嗨@darin,谢谢你。这就是我需要的,在SelectChange上的回传,但我如何连接它? – kooshka 2012-04-19 09:22:54

+0

您订阅.change事件,如我在答案中所示。这假定下拉列表在表单内。如果不是,你可以使用AJAX或使用'window.location.href'来重定向。一切都将取决于你想要达到的目标。 – 2012-04-19 11:48:35