views:

8

answers:

0

I have a simple integer property in a display model. We'll call it "MaxDays". I'm building an MVC app, and I've got the standard set of display and validation statements which will make use of the DataAnnotation attributes attached to the model:

<%: Html.LabelFor(x => x.MaxDays) %>
<%: Html.TextboxFor(x => x.MaxDays) %>
<%: Html.ValidationMessageFor(x =>x.MaxDays) %>

Now here's the hiccup: The permissible range of MaxDays varies slightly depending on whether another feature is enabled or not (that feature is flagged with a boolean property in the ViewModel as well).

So, I need to modify both DisplayName and Range attributes of the MaxDays property at run time. (Display name because they want the range included in the label: "Maximum Days (0-180)")

My first approach - clumsy, but it worked until... - was to use 2 different properties (MaxDaysIfFeatureEnabled, MaxDaysIfFeatureDisabled), each with appropriate DisplayName and Range Attributes, and select which one to display with an if statement in the view. The "until..." came when I added the Required attribute, because now they are both required, but only one of them is populated, so the form can't pass validation.

So I'm wondering if I can, in the course of building the ViewModel, modify the MetaData at run time to reflect the appropriate range? Is the MetaData per instance, and is it writeable?

Or, if anybody else has any ideas of how to design for this particular require, I'd love some hints?