Formatting dates and numbers

If you need the user to enter dates or numeric values, you may need a user-friendly formatting for these values.

The formatting uses the culture in which the HTTP request was processed. See the Multi-language applications chapter for more details.

Formatting values

The Literal, TextBox, and other controls can specify a FormatString property. If you need to output a date or number value in the page, you can use the following syntax:

<dot:Literal Text="{value: BirthDate}" FormatString="dd/MM/yyyy" />
<dot:Literal Text="{value: TotalPrice}" FormatString="c2" />

DotVVM can also translate the ToString method on date and numeric types:

<p>Total price: {{value: TotalPrice.ToString("c2")}}</p>

DotVVM uses the same format string syntax you know from C#, with the following limitations:

Editing formatted values

You can enforce the date or number format in the TextBox control using the FormatString property.

When the user enters a value in such control, it will be parsed based on the current culture, and re-formatted in case the entered value didn't follow the format strictly. See the validation chapter for more information on how to validate correctness of user-entered values.

<dot:TextBox Text="{value: BirthDate}" FormatString="d" />
<dot:TextBox Text="{value: TotalPrice}" FormatString="n2" />

Validate numeric and date values

If the user enters a value that cannot be parsed, DotVVM will try to set null in the property:

  • If the property supports null values (e. g. uses the int? type), it will get the default value on the server (e.g. 0 for int type).

  • If the property doesn't allow null values (e. g. uses the int type), a validation error will be produced when a command is triggered.

You can use the Required attribute to validate numeric and DateTime values. If the value cannot be parsed, the client-side Required validator reports an error because it sees the null value in the property. See the validation chapter for more information on how to indicate validation errors.

See also