如何制作@Ajax.DropDownList的等价物

本文关键字:等价物 DropDownList @Ajax 何制作 | 更新日期: 2023-09-27 18:06:49

我在我的MVC3/Razor应用程序中有一个局部视图,用于分页网格,目前工作得相当好。当我对它进行ajax化时,我转换了所有的@Html。ActionLink调用@Ajax.ActionLink。现在我想添加一个下拉列表,但是@Html。DropDownList不会导致AJAX部分返回,也没有@Ajax.DropDownList。

我该怎么做才能让这个下拉菜单在更改时返回?

Edit:根据偏好,我可以将其写入我自己的@Ajax。下拉列表助手将是最好的。我确信下面基于jquery的解决方案是有效的,如果有必要我会使用它们,但我确信我将在其他地方需要这个功能,并且我不希望所有这些小脚本到处浮动。

如何制作@Ajax.DropDownList的等价物

您可以使用一个普通的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);
            }
        });
    });
});

使用jQuery,您可以像这样触发更改事件,然后提交表单或执行所需的AJAX post返回到所需的路由。

<script type="text/javascript">
    $("#dropDownList").change(function() {
        // your code here
     });
</script>

这是我想到的——它最初是受到Darin的答案的启发,但我把它带到了一个完全不同的方向。

    public static MvcHtmlString DropDownList(this AjaxHelper html, string action, RouteValueDictionary routeValues, AjaxOptions options, IEnumerable<SelectListItem> selectItems, IDictionary<string, object> listHtmlAttributes)
    {
        var url = new UrlHelper(html.ViewContext.RequestContext);
        // Wrap it in a form
        var formBuilder = new TagBuilder("form");

        //  build the <select> tag
        var listBuilder = new TagBuilder("select");
        if (listHtmlAttributes != null && listHtmlAttributes.Count > 0) listBuilder.MergeAttributes(listHtmlAttributes);
        StringBuilder optionHTML = new StringBuilder();
        foreach (SelectListItem item in selectItems)
        {
            var optionBuilder = new TagBuilder("option");
            optionBuilder.MergeAttribute("value", item.Value);
            optionBuilder.InnerHtml = item.Text;
            if (item.Selected)
            {
                optionBuilder.MergeAttribute("selected", "selected");
            }
            //optionBuilder.Attributes["onchange"] = "($this.form).attr('action', '" + url.Action(action, routeValues).Replace("___", item.Value) + "');$(this.form).submit();";
            optionHTML.Append(optionBuilder.ToString());
        }
        listBuilder.InnerHtml = optionHTML.ToString();
        listBuilder.Attributes["onchange"] = "$(this.form).attr('action', '" + url.Action(action, routeValues).Replace("___", "' + $(this).first('option:selected').val() + '") + "');$(this.form).submit();";
        formBuilder.InnerHtml = listBuilder.ToString();
        foreach (var ajaxOption in options.ToUnobtrusiveHtmlAttributes())
            formBuilder.MergeAttribute(ajaxOption.Key, ajaxOption.Value.ToString());
        string formHtml = formBuilder.ToString(TagRenderMode.Normal);
        return MvcHtmlString.Create(formHtml);
    }

晚上好!我这样重写某些函数:

public static MvcHtmlString DropDownList(this AjaxHelper html, 
   string action, 
   AjaxOptions options, 
   IEnumerable<SelectListItem> selectItems, 
   IDictionary<string, object> listHtmlAttributes)

,但我不能写工作代码与HtmlAttributes。这是我的变体:

@Ajax.DropDownList("ApplSort", new AjaxOptions() { 
                         HttpMethod = "POST", 
                         InsertionMode = InsertionMode.Replace,  
                         UpdateTargetId = "target", 
                         LoadingElementId = "AjaxSearch" }, 
   new[]
   {
      new SelectListItem { Value = "0", Text = "Заявки от новых к старым" },
      new SelectListItem { Value = "1", Text = "Заявки от старых к новым" }
   }, 
   new IDictionary<string, object>  { id = "DropDownListSort", @class = "chosen" }
)

@Ajax.DropDownList("ApplSort", new AjaxOptions() { 
                         HttpMethod = "POST", 
                         InsertionMode = InsertionMode.Replace,  
                         UpdateTargetId = "target", 
                         LoadingElementId = "AjaxSearch" },
    new[]
    {
       new SelectListItem { Value = "0", Text = "Заявки от новых к старым" },
       new SelectListItem { Value = "1", Text = "Заявки от старых к новым" }
    }, 
    new  { id = "DropDownListSort", @class = "chosen" }
)

我怎样才能写得正确?

问题解决。写了两个扩展,它工作了:

public static MvcHtmlString DropDownList(this AjaxHelper html, string action, RouteValueDictionary routeValues, AjaxOptions options, IEnumerable<SelectListItem> selectItems, object htmlAttributes)
    {
        return DropDownList(html, action, routeValues, options, selectItems, new RouteValueDictionary(htmlAttributes));
    }
    public static MvcHtmlString DropDownList(this AjaxHelper html, string action, AjaxOptions options, IEnumerable<SelectListItem> selectItems, object htmlAttributes)
    {
        return DropDownList(html, action, options, selectItems, new RouteValueDictionary(htmlAttributes));
    }