查看 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 仅在绑定复杂类型时适用。