将 ExtJs 4 网格过滤器信息绑定到 asp.net mvc 操作参数的最佳方法是什么?

发布于 2024-11-10 17:05:23 字数 651 浏览 0 评论 0原文

将 ExtJs 4 Grid 过滤器信息绑定到 asp.net mvc 操作参数的最佳方法是什么?

我编写了这些辅助类:

public class ExtFilterInfo
{
    public string Field { get; set; }
    public ExtFilterData Data { get; set; }
}

public class ExtFilterData
{
    public string Type { get; set; }
    public string Value { get; set; }
}

这是操作:

public ActionResult Grid(int start, int limit, string sort, ExtFilterInfo[] filter)

QueryString 看起来像这样:

_dc:1306799668564
filter%5B0%5D%5Bfield%5D:Nome
filter%5B0%5D%5Bdata%5D%5Btype%5D:string
filter%5B0%5D%5Bdata%5D%5Bvalue%5D:nu
page:1
start:0
limit:20

What's the best way to bind ExtJs 4 Grid filter info to asp.net mvc action parameters?

I wrote these helper classes:

public class ExtFilterInfo
{
    public string Field { get; set; }
    public ExtFilterData Data { get; set; }
}

public class ExtFilterData
{
    public string Type { get; set; }
    public string Value { get; set; }
}

Here is the Action:

public ActionResult Grid(int start, int limit, string sort, ExtFilterInfo[] filter)

The QueryString looks something like this:

_dc:1306799668564
filter%5B0%5D%5Bfield%5D:Nome
filter%5B0%5D%5Bdata%5D%5Btype%5D:string
filter%5B0%5D%5Bdata%5D%5Bvalue%5D:nu
page:1
start:0
limit:20

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2024-11-17 17:05:23

自定义模型绑定器看起来可以满足要求:

public class ExtFilterInfoModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var filter = (ExtFilterInfo)base.BindModel(controllerContext, bindingContext);

        var field = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[field]");
        if (field != null)
        {
            filter.Field = field.AttemptedValue;
        }

        var type = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[data][type]");
        if (type != null)
        {
            if (filter.Data == null)
            {
                filter.Data = new ExtFilterData();
            }
            filter.Data.Type = type.AttemptedValue;
        }

        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[data][value]");
        if (value != null)
        {
            if (filter.Data == null)
            {
                filter.Data = new ExtFilterData();
            }
            filter.Data.Value = value.AttemptedValue;
        }

        return filter;
    }
}

可以在 Application_Start 中注册:

ModelBinders.Binders.Add(typeof(ExtFilterInfo), new ExtFilterInfoModelBinder());

现在控制器操作作为参数的 filter 集合应该正确绑定。

A custom model binder looks like it could fit the bill:

public class ExtFilterInfoModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var filter = (ExtFilterInfo)base.BindModel(controllerContext, bindingContext);

        var field = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[field]");
        if (field != null)
        {
            filter.Field = field.AttemptedValue;
        }

        var type = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[data][type]");
        if (type != null)
        {
            if (filter.Data == null)
            {
                filter.Data = new ExtFilterData();
            }
            filter.Data.Type = type.AttemptedValue;
        }

        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[data][value]");
        if (value != null)
        {
            if (filter.Data == null)
            {
                filter.Data = new ExtFilterData();
            }
            filter.Data.Value = value.AttemptedValue;
        }

        return filter;
    }
}

which could be registered in Application_Start:

ModelBinders.Binders.Add(typeof(ExtFilterInfo), new ExtFilterInfoModelBinder());

and now the filter collection which your controller action takes as argument should be bound correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文