【问题标题】:mvc3 webgrid filter with listbox带有列表框的 mvc3 webgrid 过滤器
【发布时间】:2012-08-22 09:56:36
【问题描述】:

我有一个启用了排序和分页的 webgrid。我在同一个视图上还有一些下拉列表和列表框来过滤 webgrid 结果。

过滤器被放置在一个带有 http get 方法的表单中。我在过滤器上应用了 jquery 来提交表单,所以每次当过滤器更改时,表单都会以所选值作为 url 中的查询字符串发回。在列表框中选择多个值时,生成的查询字符串如下所示

type=1&type=3

所以我创建了一个 int[] 类型来接受我的操作中的参数。但是,当我对 webgrid 进行排序或分页时,查询字符串会被重写为

type=1,3&page=4,

在这种情况下,类型参数变为空,列表框将被取消选择并自动使用“input-validation-error”类进行修改。

其实我更喜欢webgrid生成的querystring的风格

type=1,3

所以我可以将它直接传递到我的查询中。但是,列表框似乎不喜欢这种查询字符串。有没有办法让列表框识别组合的查询字符串,或者我必须编写代码来处理查询字符串和选定的项目?

【问题讨论】:

    标签: asp.net-mvc listbox webgrid


    【解决方案1】:

    谢谢。我最终在定制的模型活页夹中完成了类似的工作

    class MultiSelectionBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            string key = bindingContext.ModelName;
            ValueProviderResult val = bindingContext.ValueProvider.GetValue(key);
            if (val != null && !string.IsNullOrEmpty(val.AttemptedValue))
            {
                bindingContext.ModelState.SetModelValue(key, val);
    
                string incomingString = ((string[])val.RawValue)[0];
                if (incomingString.Contains(","))
                {
                    var value = new ValueProviderResult(incomingString.Split(','), string.Empty, CultureInfo.InvariantCulture);
                    bindingContext.ModelState.SetModelValue(key, value);
                    return value.ConvertTo(typeof(int[]));
                }
    
                return val.ConvertTo(typeof(int[]));
            }
    
            return null;
        }
    }
    

    并将其应用于参数

    public ViewResult Index([ModelBinder(typeof(MultiSelectionBinder))] int[] type, ...)
    

    【讨论】:

      【解决方案2】:

      看起来像 WebGrid 中的一个错误。

      我会在您的操作开始时建议以下解决方法:

      if(type == null && !string.IsNullOrWhiteSpace(Request.QueryString["type"]))
          type=Request.QueryString["type"].Split(',');
      

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 2023-03-14
        • 2012-01-12
        • 2011-09-04
        • 2012-02-15
        • 2012-09-18
        • 2012-03-03
        • 2012-05-05
        • 1970-01-01
        相关资源
        最近更新 更多