Skip to main content
 首页 » 编程设计

c#之如何制作等同于@Ajax.DropDownList

2024年02月03日33lyj

我在我的 MVC3/Razor 应用程序中有一个局部 View ,用于对当前运行良好的网格进行分页。当我对它进行 AJAX 化时,我将所有 @Html.ActionLink 调用都转换为 @Ajax.ActionLink。现在我想添加一个 DropDownList,但是 @Html.DropDownList 不会导致 AJAX 部分回发,并且没有 @Ajax.DropDownList。

我该怎么做才能让这个下拉列表在更改时回发?

编辑: 根据偏好,我可以将一些内容写入我自己的 @Ajax.DropDownList 助手中是最好的。我确定下面的基于 jQuery 的解决方案有效,如果需要我会使用它们,但我确定我会在其他地方使用此功能,并且我不希望所有这些小脚本随处可见.

请您参考如下方法:

您可以使用普通的 Html.DropDownListFor 应用一些自定义 CSS 类,然后订阅 .change 事件并手动触发 AJAX 请求:

$(function() { 
    $('.someClassYouHaveAddedToYourDdl').change(function() { 
        var page = $(this).val(); 
        $.ajax({ 
            url: '@Url.Action("SomeActionResponsibleForPagination")', 
            type: 'POST', 
            data: { page: page }, // The page parameter might need adapting 
            success: function(result) { 
                // TODO: here you could refresh the part of the DOM containing 
                // the grid with the new results. Use $('.someSelector').html(result); 
            } 
        }); 
    }); 
});