Been struggling for a few days with this one and was wondering if anyone done something similar.

At run-time, I will load a bunch of questions from the database. This will have members like Id, Type, Label, Required, RegEx, etc. So there is no way for me to know in advance what is going to be in there.
Since this project is going to grow huge I did not want to try be fancy or take shortcuts, and make things work as if the questions / types was known at design time (the normal view models with validation attributes, causing both client and server side validation to work.

Fist issue was creating "controls" based upon the type of question, load a form and posting the values. Sorted the model-binding...if you want to see the project what I got working so far download it here

Now for validation, which I partially got working by creating custom validators, but besides the error messages not being right, client-side validation also does not kick in. My problem seems to be the way I create custom editor templates I'm sure.

So I got a view model that will hold all "questions"
Code:
    public class ControlsViewModel
    {
        public ControlsViewModel()
        {
            Controls = new List<ControlViewModel>();
        }

        public List<ControlViewModel> Controls { get; set; }
    }
Then a "base " control view model and overrides for the different control types (question might have a type of "TextBox", "DropDown", "Checkbox", etc)
Code:
    public abstract class ControlViewModel
    {
        public abstract string Type { get; } // in derived classes this might be a boolean, date, etc
        public int Id { get; set; } // database Id of the question..used to "Map back" later
        public string Label { get; set; } // what the label should say 
        public string Name { get; set; } // figured use this for name/id of the control
        public bool Required { get; set; } // if value is required
        public string RegEx { get; set; } // if present, value need match regular expression
    }

    public class TextBoxViewModel : ControlViewModel
    {
        public override string Type
        {
            get { return "TextBox"; }
        }

        public string Value { get; set; }
    }
For now I just have a static class, filling up a collection of questions and return a view with that collection as view model:
Code:
@model QuestionsDemo.Models.ControlsViewModel
@using (Html.BeginForm())
{
    for (int i = 0; i < Model.Controls.Count; i++)
    {
        <div>
            @Html.HiddenFor(x => x.Controls[i].Type)
            @Html.HiddenFor(x => x.Controls[i].Name)
            @Html.EditorFor(x => x.Controls[i])
        </div>
    }
    <button type="submit" class="btn">Submit</button>
}
Now, for each of the derived control types (let's stick to "") I got an Editor Template (TextBoxViewModel.cshtml in Shared/EditorTemplates)
Code:
@model QuestionsDemo.Models.TextBoxViewModel
<div>
    @Html.HiddenFor(x => x.Label)
    @Html.HiddenFor(x => x.Id)
    @Html.LabelFor(x => x.Value, Model.Label)
    @Html.TextBoxFor(x => x.Value)
    @Html.ValidationMessageFor(x=>x)
</div>
Then a model binder I created (not crazy about the magic strings, but this is still a demo, so let that fly for now)
Code:
    public class ControlsModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext,Type modelType)
        {
            ValueProviderResult type = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Type");
            object model =Activator.CreateInstance("QuestionsDemo","QuestionsDemo.Models." + type.AttemptedValue + "ViewModel").Unwrap();
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model,model.GetType());
            return model;
        }
    }
With this all in place, the form render, correct controls and model bind fine when I submit the form to the post action method. (in the sense of receiving a list of controls along with whatever properties was in the editor templates)

Don't know if the above was the best approached, but went on with the next problem, namely validation:
Code:
   public class TextBoxControlValidationAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var control = value as TextBoxViewModel;
            if (control != null)
            {
                string controlValue = control.Value;
                control =
                    QuestionFactory.Questionaire.Controls.FirstOrDefault(x => x.Id == control.Id) as TextBoxViewModel;
                if (control != null)
                {
                    if (control.Required && string.IsNullOrEmpty(controlValue))
                        return false;
                    if (!string.IsNullOrEmpty(control.RegEx) && !Regex.IsMatch(control.Value, control.RegEx))
                        return false;
                }
            }
            return true;
        }

        public override string FormatErrorMessage(string name)
        {
            return string.Format("{0} missing or invalid....1 msg for all rules????");
        }
    }
and decorated my TextBoxViewModel with this one to see how thing pan out:
Code:
    [TextBoxControlValidation]
    public class TextBoxViewModel : ControlViewModel
    {
       ......
So that's what I got so far.

The good:
Model Binding works
Validation works

The bad:
Client Side Validation does not work
Error messages cannot be set
...hell I don't even know.

Any ideas or suggestions?