Part 3 Enumeration classes - EnumerationQueryStringModelBinderOfT
public class EnumerationQueryStringModelBinder<T> : IModelBinder | |
where T : Enumeration | |
{ | |
public Task BindModelAsync(ModelBindingContext bindingContext) | |
{ | |
if (bindingContext == null) | |
{ | |
throw new ArgumentNullException(nameof(bindingContext)); | |
} | |
var enumerationName = bindingContext.ValueProvider.GetValue(bindingContext.FieldName); | |
if (string.IsNullOrEmpty(enumerationName.FirstValue)) | |
{ | |
bindingContext.Result = ModelBindingResult.Success(default(T)); | |
} | |
else if (Enumeration.TryGetFromValueOrName<T>(enumerationName.FirstValue, out var result)) | |
{ | |
bindingContext.Result = ModelBindingResult.Success(result); | |
} | |
else | |
{ | |
bindingContext.Result = ModelBindingResult.Failed(); | |
bindingContext.ModelState.AddModelError(nameof(bindingContext.FieldName), | |
$"{enumerationName.FirstValue} is not supported."); | |
} | |
return Task.CompletedTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment