@@ -36,38 +36,3 @@ public class ModelActionFilter(IStringLocalizer _localizer) : ActionFilterAttrib
|
||||
|
||||
|
||||
|
||||
|
||||
public class SpecificNullableBinderProvider : IModelBinderProvider
|
||||
{
|
||||
public IModelBinder? GetBinder(ModelBinderProviderContext context)
|
||||
{
|
||||
var type = context.Metadata.ModelType;
|
||||
|
||||
// 只处理 Guid? 和 int?
|
||||
if (type == typeof(Guid?) || type == typeof(int?))
|
||||
{
|
||||
return new SpecificNullableBinder();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class SpecificNullableBinder : IModelBinder
|
||||
{
|
||||
public Task BindModelAsync(ModelBindingContext context)
|
||||
{
|
||||
var value = context.ValueProvider.GetValue(context.ModelName).FirstValue;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
context.Result = ModelBindingResult.Success(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Result = ModelBindingResult.Success(value);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user