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

时间:2011-08-26 14:19:30

标签: c# asp.net-mvc-3 razor

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

如何让此下拉列表重新发布更改?

编辑:根据偏好,我可以写入我自己的@ Ajax.DropDownList帮助器的东西最好。我确信下面基于jQuery的解决方案有效,如果有必要我会使用它们,但我确定我会在其他地方想要这个功能,而且我宁愿不要把所有这些小脚本都放在一边

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

     });
</script>

答案 2 :(得分:1)

这就是我想出的 - 它最初的灵感来自达林的答案,但我把它带到了一个完全不同的方向。

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

答案 3 :(得分:1)

晚上好!我以这种方式重写某些功能:

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