ISelectListValueProvider
The "Checkbox list", "Radio button list" and "Select (drop-down) field" form components use the ISelectListValueProvider interface to determine which values to show to the user. If you want to populate anyone of those form components which custom values which are generated in code or by any other means, you can implement your own ISelectListValueProvider. The only method you need to implement is the GetValues() method which returns a dictionary of keys and values.
Interface declaration
public interface ISelectListValueProvider { Task<Dictionary<string, string>> GetValues(SelectListValueProviderContext context); }
Example
In the following example we have a value list provider which automatically populates the form component with years. The user can specify the start and to year, or just how many years back from the current year are shown.
[Description("Populates a select list with years ranging from the given start/end year and/or the current year")] [DisplayName("Years")] public class YearSelectListValueProvider : ISelectListValueProvider { [EditorBrowsable] [DisplayName("Years back")] [Description("The number of years to show from the current year and back")] public int? YearsBack { get; set; } [EditorBrowsable] [DisplayName("Year from")] [Description("The starting year to show")] public int? YearFrom { get; set; } [EditorBrowsable] [DisplayName("Year to")] [Description("The end year to show")] public int? YearTo { get; set; } [EditorBrowsable] [DisplayName("Placeholder")] [Description("The default text which is shown when nothing is selected")] public string Placeholder { get; set; } public Task<Dictionary<string, string>> GetValues(SelectListValueProviderContext context) { var result = new Dictionary<string, string>(); result.Add("", Placeholder); var startYear = YearFrom.HasValue ? YearFrom.Value : DateTime.Today.Year - (YearsBack.HasValue ? YearsBack.Value : 0); var endYear = YearTo.HasValue ? YearTo.Value : DateTime.Today.Year - (YearFrom.HasValue ? (YearsBack.HasValue ? YearsBack.Value : 0) : 0); for (var year = endYear; year >= startYear; year--) { result.Add(year.ToString(), year.ToString()); } return Task.FromResult(result); } }
Registration
This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).
public void ConfigureServices(IServiceCollection services, Microsoft.Extensions.Configuration.IConfiguration configuration) { services.AddNamed<ISelectListValueProvider, YearSelectListValueProvider>("YearSelectListValueProvider", ServiceLifetime.Transient); }