2
如您所知,在WebForm中觸發事件非常容易,但這是MVC框架中的一個問題。例如,我有2個DropDownList,Country
和State
。我想根據選定的Country
加載State
中的數據。在WebForm中,我可以觸發SelectedIndexChange
事件,但在MVC框架中,我該怎麼做?MVC框架中的觸發器事件
請幫忙。提前致謝。
如您所知,在WebForm中觸發事件非常容易,但這是MVC框架中的一個問題。例如,我有2個DropDownList,Country
和State
。我想根據選定的Country
加載State
中的數據。在WebForm中,我可以觸發SelectedIndexChange
事件,但在MVC框架中,我該怎麼做?MVC框架中的觸發器事件
請幫忙。提前致謝。
在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>
請注意,上面的例子依賴於[jQuery庫(http://jquery.com/)。 –
我可以建議該示例使用'.on('change',...)'? –
@ErikSchierboom,謝謝你指出。指出。 –