【问题标题】:How to make the equivalent of @Ajax.DropDownList?如何制作相当于@Ajax.DropDownList?
【发布时间】:2012-11-22 23:06:05
【问题描述】:

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

我可以做些什么来让这个下拉列表回复更改?

编辑: 根据偏好,我可以在自己的@Ajax.DropDownList 助手中写入一些内容是最好的。我确信下面的基于 jQuery 的解决方案可以工作,如有必要我会使用它们,但我确信我会在其他地方想要这个功能,而且我宁愿不要让所有这些小脚本四处飘荡.

【问题讨论】:

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


    【解决方案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);
                }
            });
        });
    });
    

    【讨论】:

    • 这绝对看起来可行,如果没有更好的选择,我会实现它,但我希望可以将一些东西放入 HTML Helper。我已经编辑了问题以反映这一点。
    • @Bobson,你最好将我展示的代码外部化为custom jQuery plugin。您不需要编写 HTML 帮助程序,因为它已经存在:Html.DropDownListFor。一旦你有了 jquery 插件,你只需将它附加到你的 WebGrid。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案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);
          }
      

      【讨论】:

        【解决方案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));
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-07
          • 2019-11-30
          • 2013-03-07
          • 2023-04-04
          相关资源
          最近更新 更多