如何使@ Ajax.DropDownList的相同呢?(How to make the equiva

2019-09-28 00:11发布

我在我的MVC3 /剃须刀的应用程序用于呼叫目前运作得相当好网格的局部视图。 当我支持Ajax,我转换所有的@ Html.ActionLink调用@ Ajax.ActionLink。 现在,我想添加一个DropDownList,但@ Html.DropDownList不会导致AJAX部分回发,而且也没有@ Ajax.DropDownList。

我能做些什么来得到这个下拉菜单后回到变化?

编辑:作为优选,东西,我可以写在了自己@ Ajax.DropDownList帮手将是最好的。 我敢肯定,下面的工作基于jQuery的解决方案,并在必要时我会使用它们,但我敢肯定,我要去其他地方想要这个功能,我宁愿没有这些小脚本左右浮动。

Answer 1:

你可以使用一个正常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);
            }
        });
    });
});


Answer 2:

使用jQuery你可以解雇change事件像这样,然后要么提交表单或执行所需的AJAX回发到你想要的路线。

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

     });
</script>


Answer 3:

以下是我想出了 - 它最初是由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);
    }


Answer 4:

晚上好! 我重写这样某些功能:

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));
    }


文章来源: How to make the equivalent of @Ajax.DropDownList?