DefaultModelBinder のソース コードを調べたところ、ModelMetadata.ConvertEmptyStringToNull プロパティは、バインドされているモデルが複雑なモデルの場合にのみチェックされることがわかりました。アクションで公開されるプリミティブ型の場合、値はそのまま取得されます。元の質問の例では、これは空の文字列になります。
DefaultModelBinder ソースから
// Simple model = int, string, etc.; determined by calling TypeConverter.CanConvertFrom(typeof(string))
// or by seeing if a value in the request exactly matches the name of the model we're binding.
// Complex type = everything else.
if (!performedFallback) {
bool performRequestValidation = ShouldPerformRequestValidation(controllerContext, bindingContext);
ValueProviderResult vpResult = bindingContext.UnvalidatedValueProvider.GetValue(bindingContext.ModelName, skipValidation: !performRequestValidation);
if (vpResult != null) {
return BindSimpleModel(controllerContext, bindingContext, vpResult);
}
}
if (!bindingContext.ModelMetadata.IsComplexType) {
return null;
}
return BindComplexModel(controllerContext, bindingContext);
}
したがって、私が知る限り、ModelMetadata.ConvertEmptyStringToNull は複合型をバインドする場合にのみ適用されます。