如何更新自定义 ModelBinder 以使用 ASP.NET MVC RC2 中更改的 ModelBindingContext.ValueProvider 接口

发布于 2024-08-23 02:09:49 字数 1305 浏览 5 评论 0原文

我有一个自定义模型绑定器,它采用逗号分隔的列表并清除任何空值,然后将其传递给默认模型绑定器。这在 ASP.NET MVC Preview 2 中有效,但是当我升级到 RC2 时,下面的代码将无法编译,因为 ValueProvider 的接口只有 GetValue() 方法,没有 [] 访问器。我在下面所做的事情是否可以通过绑定上下文中的其他机制实现?我宁愿不必为这种简单的情况创建一个完整的模型绑定器。主要目标是当值绑定到 List时,将跳过任何空值。

public class EnumListModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var result = bindingContext.ValueProvider[bindingContext.ModelName];
        string[] rawValues = (string[])result.RawValue;
        var newValues = new List<string>();
        foreach (string value in rawValues)
        {
            if (!String.IsNullOrEmpty(value))
            {
                newValues.Add(value);
            }
        }

        string newValuesAttempted = String.Join(",", newValues.ToArray());
        // overwrite the ValueProviderResult with the cleaned up csv list
        // this is the part I'm not sure how to implement using the interface
        bindingContext.ValueProvider[bindingContext.ModelName] = 
           new ValueProviderResult(newValues.ToArray(), newValuesAttempted, result.Culture);

        return System.Web.Mvc.ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, bindingContext);
    }
}

I have a custom model binder that takes a comma separated list and cleans out any empty values, then passes it along to the default model binder. This worked in ASP.NET MVC Preview 2, but when I upgraded to RC2, the below won't compile because the interface of ValueProvider only has a GetValue() method, no [] accessor. Is what I'm doing below possible through some other mechanism in the binding context? I'd rather not have to create a full blown model binder for such a simple situation. The main goal is when the values are bound to a List<SomeEnum>, any empty values are skipped.

public class EnumListModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var result = bindingContext.ValueProvider[bindingContext.ModelName];
        string[] rawValues = (string[])result.RawValue;
        var newValues = new List<string>();
        foreach (string value in rawValues)
        {
            if (!String.IsNullOrEmpty(value))
            {
                newValues.Add(value);
            }
        }

        string newValuesAttempted = String.Join(",", newValues.ToArray());
        // overwrite the ValueProviderResult with the cleaned up csv list
        // this is the part I'm not sure how to implement using the interface
        bindingContext.ValueProvider[bindingContext.ModelName] = 
           new ValueProviderResult(newValues.ToArray(), newValuesAttempted, result.Culture);

        return System.Web.Mvc.ModelBinders.Binders.DefaultBinder.BindModel(controllerContext, bindingContext);
    }
}

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

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

发布评论

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

评论(2

南渊 2024-08-30 02:09:49

在这里使用 GetValue() 而不是 [] 到底有什么问题?它做同样的事情。但是 ValueProvider 现在是一个接口,并且接口不能有索引器。因此,GetValue()

将您的代码更改为:

var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

不过,我有点惊讶下一行的强制转换竟然有效。似乎高度依赖于用户实际提交的内容以及特定的属性类型是什么。不过,这与你的问题无关。

What, exactly, is wrong with using GetValue() instead of [] here? It does the same thing. But ValueProvider is an interface now, and interfaces can't have indexers. Hence, GetValue().

Change your code to:

var result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

I'm a little surprised that the cast on the next line ever worked, though. Seems highly dependent on what the user actually submits and what the particular property type is. That's unrelated to your question, though.

鱼忆七猫命九 2024-08-30 02:09:49

由于 ValueProvider 集合是只读的,所以这里没有太多的解决方案。相反,我最终使用了自定义模型绑定器

有没有办法让 DefaultModelBinder 在绑定到 List时忽略空项

There wasn't much of a solution here now that the ValueProvider collection is readonly. Instead I ended up using a custom model binder

Is there a way to have the DefaultModelBinder ignore empty items when binding to a List<Enum>

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