Using DataAnnotations to validate DynamicContent fields


I have created a simple DynamicContent plugin by decorating my usercontrol with the DynamicContentPlugIn-attribute, and my public properties are automatically exposed to the editor as approprioate input boxes (depending on each property's type), as expected. SO far so good. Now I want to extent the input validation.

I want my property to be required, and I want to limit the range of valid integers that can be entered by the editor.

Is there a simple way of doing this? I was hoping for DataAnnotations-support, as described in this blog post for developing Visitor Criterias (, but that does not seem to work..

Am I really forced to write a while separate edit-usercontrol that inherits from DynamicContentEditControl, just to implement this simple task (as examplified in this post by Fredrik Haglund:

Please tell me I am missing something.. :-)

Aug 25, 2011 22:49

Unfortunately I don't think that it's anything else but visitor groups that support data annotations for validation at the moment, unless I have missed something. We are currently working to improve this to allow data annotations as a simpler way of defining the requirements for your properties.

Linus Ekström
EPiServer Development Team 

Aug 26, 2011 7:20

I wrote a short post about using data annotations for validating a page upon save here. Perhaps that's something that can be of use?

Aug 26, 2011 7:47


I ended up creating my own edit-usercontrol, with the custom controls provided by the EPIServer Template Foundation project on Codeplex ( After modifying their controls by decorating the classes with [ValidationProperty("propertyname")], I could use standard to validate their values in my edit-usercontrol. So far so good.

Now I face the problem of accessing the properties exposed by my view-usercontrol from codebehind in my edit-usercontrol. I just can't find a way of reaching them with strongly typed code!

What I tried was this:

((MyViewControl)Content).Width = txtHeight.Text;

But this caused a runtime error, because "Content" wasn't of the correct type. It was of type DynamicContentAdapter<MyViewControl>.

So instead, had to write with code that looks a few years old, with the name of the property in quotes (*shudder*):

Content.Properties["Width"].Value = txtHeight.Text;

Can anyone see what I am doing wrong? How can I access the properties directly through the Content-property exposed by DynamicContentEditControl?


Aug 30, 2011 10:55
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.