如何使@ Ajax.DropDownList等效? [英] How to make the equivalent of @Ajax.DropDownList?

查看:77
本文介绍了如何使@ Ajax.DropDownList等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a partial view in my MVC3/Razor app for paging a grid which works fairly well currently. When I AJAXified it, I converted all the @Html.ActionLink calls to @Ajax.ActionLink. Now I want to add a DropDownList, but @Html.DropDownList doesn't cause the AJAX section to post back, and there's no @Ajax.DropDownList.

我该怎么做才能使此下拉列表重新发布更改?

What can I do to get this dropdown to post back on change?

根据喜好,最好将其写入我自己的@ Ajax.DropDownList帮助器中.我确定下面的基于jQuery的解决方案可以正常工作,并且在必要时会使用它们,但是我确定我会在其他地方希望使用此功能,并且我不希望所有这些小脚本都无处不在

By preference, something that I can write into my own @Ajax.DropDownList helper would be best. I'm sure the jQuery-based solutions below work, and I'll use them if necessary, but I'm sure I'm going to want this functionality elsewhere, and I'd prefer to not have all these little scripts floating around.

推荐答案

这就是我想出的-它最初是由达林的答案启发的,但我却朝着完全不同的方向前进.

Here's what I came up with - it was originally inspired by Darin's answer, but I took it in a completely different direction.

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

这篇关于如何使@ Ajax.DropDownList等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆