'In Expressive Annotations using the AssertThat attribute - how to modify the error message at runtime or pass in string values

In Expressive Annotations, using the "AssertThat" attribute is it possible to modify the error message at runtime? I am working in ASP.NET MVC Classic environment and needed to add to my existing validations. The new validation would check that the date entered was within a specific date range and I wanted to incorporate the boundary date in the error message but the date is not a constant and can change (every 3-4 months). The issue is that the "AssertThat" ErrorMessage attribute only seems to allow constant strings.

The code looks something like this

[AssertThat ("!IsDateBeforeDataRange(BeginDate.Value)"
, ErrorMessage ="Requested Date is prior to data ingest range ")]
public DateMvcModel BeginDate { get; set; }

This works, but I wanted to incorporate the actual date boundary in the error message like this:

[AssertThat ("!IsDateBeforeDataRange(BeginDate.Value)"
, ErrorMessage ="Requested Date is prior to data ingest range{IngestDataDtBegnin} ")] 

But this is not allowed. In the examples for "AssertThat" the default error message does incorporate string interpolation, but I can't figure how to pass my own values in.



Solution 1:[1]

A bit late for the OP, but yes this can be done, using the curly bracket syntax in your second snippet.

The trick is the date has to be stored in a property, and posted into the action.

If the date property is alresdy used in your view, it should just work, but if it is a value that is not currently displayed in your view, you'll need to store it in a hidden field so that the value will be available for both client side and server side.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ozz