public static bool TryGetContractKind(this ModelBindingContext bindingContext, string name, ref bool hasValue, out int?result) { result = null; var valueObject = bindingContext.ValueProvider.GetValue(name); if (valueObject != ValueProviderResult.None) { bindingContext.ModelState.SetModelValue(name, valueObject); if (!string.IsNullOrEmpty(valueObject.FirstValue)) { if (!ContractKinds.TryParse(valueObject.FirstValue, out var kind)) { bindingContext.ModelState.TryAddModelError(name, "Invalid contract kind."); return(false); } hasValue = true; result = kind; } } return(true); }
public static bool TryGetContractKindList(this ModelBindingContext bindingContext, string name, ref bool hasValue, out List <int> result) { result = null; var valueObject = bindingContext.ValueProvider.GetValue(name); if (valueObject != ValueProviderResult.None) { bindingContext.ModelState.SetModelValue(name, valueObject); if (!string.IsNullOrEmpty(valueObject.FirstValue)) { var rawValues = valueObject.FirstValue.Split(',', StringSplitOptions.RemoveEmptyEntries); if (rawValues.Length == 0) { bindingContext.ModelState.TryAddModelError(name, "List should contain at least one item."); return(false); } hasValue = true; result = new List <int>(rawValues.Length); foreach (var rawValue in rawValues) { if (!ContractKinds.TryParse(rawValue, out var kind)) { bindingContext.ModelState.TryAddModelError(name, "List contains invalid contract kind."); return(false); } hasValue = true; result.Add(kind); } } } return(true); }