Search K
Appearance
A CurrencyField
is useful in scenarios when you want to store numeric values related to a currency, such as product prices. Its underlying type argument is a decimal
, which is used for high-precision numeric values.
Each CurrencyField
is associated with a currency. Its initialization method requires an additional argument:
CurrencyField.Make(string label, string name, string currencyIsoCode)
.
The third argument currencyIsoCode
refers to the three-letter ISO 4217 codes associated with regional currencies. For example, 'USD' for US Dollars, 'CHF' for Swiss Francs or 'CAD' for Canadian Dollars.
In our demo application we use a decimal property Price
to store purchase prices for our camping products.
public class Product
{
[Key]
public int Id { get; init; }
public decimal Price { get; set; }
}
We can then represent this property with a CurrencyField
in the property resource. Here we use the currencyIsoCode
EUR to display the value as Euros.
public override IList<IResourceFieldBuilder> Fields()
{
return
[
CurrencyField.Make("Purchase Price", nameof(Product.Price), "EUR"),
];
}
A CurrencyField
is displayed with its value and its associated currency symbol. The currency will be formatted according to the browser's preferred language.
In Create and Update views a user will see a numeric input field associated with the configured currency.
The default visibility values are as follows:
Resource View | Default |
---|---|
Index | visible |
Create | visible |
Update | visible |
Details | visible |
You can change the visibility of the field for each view. Please note, however, that fields should remain visible on Create and Update pages if the field is required by the entity model and if no default value is set.
A CurrencyField
can be made sortable by chaining a call to Sortable()
after adding the field. Sorting will be performed numerically.
public override IList<IResourceFieldBuilder> Fields()
{
return
[
CurrencyField.Make("Purchase Price", nameof(Product.Price), "EUR")
.Sortable(),
];
}
We provide a default filter for currency fields. Simply chain a Filterable()
method call after the field definition. The filter will allow users to filter by minimum and maximum values.
public override IList<IResourceFieldBuilder> Fields()
{
return
[
CurrencyField.Make("Purchase Price", nameof(Product.Price), "EUR")
.Filterable(),
];
}