如何更新自定义 ModelBinder 以使用 ASP.NET MVC RC2 中更改的 ModelBindingContext.ValueProvider 接口
我有一个自定义模型绑定器,它采用逗号分隔的列表并清除任何空值,然后将其传递给默认模型绑定器。这在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里使用
GetValue()
而不是[]
到底有什么问题?它做同样的事情。但是 ValueProvider 现在是一个接口,并且接口不能有索引器。因此,GetValue()
。将您的代码更改为:
不过,我有点惊讶下一行的强制转换竟然有效。似乎高度依赖于用户实际提交的内容以及特定的属性类型是什么。不过,这与你的问题无关。
What, exactly, is wrong with using
GetValue()
instead of[]
here? It does the same thing. ButValueProvider
is an interface now, and interfaces can't have indexers. Hence,GetValue()
.Change your code to:
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.
由于 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>