Option Helper

General information

"OptionHelper.cs" is the class that enables the population of Dropdown Lists that have fixed values. It reads the List of Values from the "SelectionOption" Data Table based on their "LOV" (List of Values) Key that is found in the "Name" column of the data table.

Selection Option data table

All the values of fixed Dropdown Lists are stored in the "SelectionOption" data table. As with all data tables, the columns are defined in a class of the same name under the "Entities" directory and the records can be accessed via the SQL Server Management Studio (SSMS).

The "SelectionOption" data table consists of 7 columns: ID, Value, Text, Name, Position, ExtDesc, and Language.

  • The ID column contains numbers of 5 digits that is used to clearly identify each value. When a new one is added, the ID simply is the value of the last entry incremented by 1.

  • The Value column contains the fixed values.

  • The Text column contains the text that would get displayed in the Dropdown List representing the value.

  • The Name column contains the LOV Key by which the fixed values are mapped to the correct Dropdown List. All LOV Keys are defined in "ConstantKey.cs", so if you want to add a new fixed Dropdown, you need to add the corresponding Key in "ConstantKey.cs"). If you want to change an existing LOV Key, you also do that in "ConstantKey.cs".

  • The Position column is used to set the order in which the values should be displayed in the Dropdown by using numbers (conventionally in steps of 10).

  • The ExtDesc column is provided for the case that it makes sense to add a description.

  • The Language column is used for the case that a Dropdown List should have the option of getting displayed in another language. The default language used is "en-US".

So if you want to add a new fixed Dropdown List, you define a new LOV Key in the "ConstantKey.cs" and add the fixed values in the "SelectionOption" Data Table inserting the LOV Key value in the column "Name". If you want to change the values of an existing fixed Dropdown, you do that in the "SelectionOption" Data Table.

Option Helper class

Instance

The Option Helper class only contains an instantiation of OptionHelperBase<IUnitWork> inserting the "MiddleContext.Unitwork" as parameter.

"OptionHelperBse.cs" is a class that provides functions to fetch all and specific values from the "SelectionOption" data repository (located in "UnitWork" like all data repositories).

Get method

The "OptionHelper.cs" file contains another class "KeyValueHelperBaseExtension" that consists of only one method "Get". It is the function used to fetch the correct List of Values.

"Get" returns a List of SelectListItems and takes the instance of the Option Helper and a string "name" as parameter. "name" is the parameter that accepts the Macro string of the LOV Key that is needed to fetch the correct values. It also considers the Language of the system when fetching the records from the "SelectionOption" data repository.

How to use

When you want to use a fixed Dropdown in a webpage, do the following:

  1. Define a public Property in the Model class that is of data type "List<SelectListItem>"

  2. Initialize the List by calling "OptionHelper.Instance.Get()" inserting the corresponding LOV Key Macro preceded by "ConstantKey." as parameter.

  3. Add a Dropdown Control Input in the View file, where you want it to be, inserting the List from the Model (converted into an MVC List) as parameter.

The following codes illustrate the concrete implementation of the above described steps:

public List<SelectListItem> SupplierList { get; set; }
this.SupplierList = OptionHelper.Instance.Get(ConstantKey.LOVSUPPLIER);
<td>@Html.DropDownListFor(mdl => mdl.Header.Vendor, @Converter.ToMvcList(Model.SupplierList), new { @class = "text100" })</td>

Last updated