是否可以使用ModelBinderAttribute
绑定到集合?MVC 3 ModelBinding使用ModelBinderAttribute的集合
这里是我的操作方法参数:
[ModelBinder(typeof(SelectableLookupAllSelectedModelBinder))] List<SelectableLookup> classificationItems
这是我的自定义模型粘合剂:
public class SelectableLookupAllSelectedModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = bindingContext.Model as SelectableLookup ??
(SelectableLookup)DependencyResolver.Current.GetService(typeof(SelectableLookup));
model.UId = int.Parse(bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue);
model.InitialState = true;
model.SelectedState = true;
return model;
}
}
下面是该参数的发布JSON数据:
"classificationItems":["19","20","21","22"]}
以下是ValueProvider看到它的方式:
viewModel.classificationItems[0]
AttemptedValue = “19” viewModel.classificationItems[1]
AttemptedValue = “20” viewModel.classificationItems[2]
AttemptedValue = “21” viewModel.classificationItems[3]
AttemptedValue = “22”
当前不工作,因为首先有一个前缀( “视图模型” )我可以理清,但其次bindingContext.ModelName
是“classificationItems”这是参数的名称被绑定到,而不是列表中的索引项,即“分类项[0]”
我应该补充说,当我将此资料夹声明为全局ModelBin der in global.asax it works fine ...
更改动作方法的签名接收一个收集成员的对象。 – 2013-02-13 12:50:25