を使用してコレクションにバインドすることは可能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"
これは現在機能していません。これは、最初に並べ替えることができるプレフィックス (「viewModel」) があるためですが、次に、bindingContext.ModelName
バインドされているパラメーターの名前であり、リスト内のインデックス付きアイテムではない「classificationItems」、つまり「classificationItems」であるためです。 [0]"
このバインダーをglobal.asaxでグローバルModelBinderとして宣言すると、正常に動作することを追加する必要があります...