2013-04-02 46 views
2

如您所知,在WebForm中觸發事件非常容易,但這是MVC框架中的一個問題。例如,我有2個DropDownList,CountryState。我想根據選定的Country加載State中的數據。在WebForm中,我可以觸發SelectedIndexChange事件,但在MVC框架中,我該怎麼做?MVC框架中的觸發器事件

請幫忙。提前致謝。

回答

6

在WebForm中,我可以觸發SelectedIndexChange事件,但在MVC 框架中,我應該怎麼做?

您可以使用javascript並訂閱onchange下拉菜單的javascript事件。

例如,如果你使用jQuery:

<script type="text/javascript"> 
    $(function() { 
     $('#id_of_your_drop_down').on('change', function() { 
      // the value of the dropdown changed. Here you could do whatever 
      // you intended to do. For example you could send the selected value 
      // to a controller action using an AJAX call. 
      var selectedValue = $(this).val(); 
      var url = '@Url.Action("SomeAction")'; 
      $.post(url, { value: selectedValue }, function(result) { 
       // The AJAX request completed successfully. Here you could 
       // do something with the results returned by the server 
      }); 
     }); 
    }); 
</script> 
+0

請注意,上面的例子依賴於[jQuery庫(http://jquery.com/)。 –

+0

我可以建議該示例使用'.on('change',...)'? –

+0

@ErikSchierboom,謝謝你指出。指出。 –