MVC框架中的触发事件

Tri*_*oan 2 c# asp.net asp.net-mvc-3

正如您所知,在WebForm中触发事件非常容易,但这是MVC框架中的一个问题.例如,我有2个DropDownList,CountryState.我想State在所选的基础上加载数据Country.在WebForm中,我可以触发SelectedIndexChange事件,但在MVC框架中,我该怎么办?

请帮忙.提前致谢.

Dar*_*rov 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>
Run Code Online (Sandbox Code Playgroud)

  • 不,当然不.这是一个客户端事件,订阅它的唯一方法是使用javascript.顺便说一下,这也是WebForms世界中实现的方式.不要以为在WebForms中它有任何不同.它们只是HTTP和HTML的漏洞抽象.在一天结束时,您将提供客户端浏览器HTML.考虑到这一点.不是服务器端框架. (2认同)