MVC Dynamic Forms and a LINQ or EF Model

Apr 2, 2010 at 2:33 PM


Do you have an example of using MVC Dynamic Forms with a LINQ or EF based repository class?


Apr 2, 2010 at 3:00 PM

No. But, the source code download with the demo shows how to construct the form and fields. It doesn't matter where the form field definitions are stored. In my next release I will include a database that stores the field definitions and I'll use either Linq to SQL or Entity Framework to demonstrate how to build a form from a database.

Jul 28, 2010 at 7:12 AM


I am currently working on an application where I need to create forms on the fly, based on information stored in a database. I am confident that the Dynamic Forms library can help me get the job done. Since I am still pretty new to .net and MVC, could anyone give me an example of how to create a from from a database? In the demo all form fields are hardcoded in FormProvider.cs. But how would this be done when the definitions come from a database?

If you can point me in the right direction, I would be ever so grateful.




Jul 28, 2010 at 3:11 PM

Instead of creating the field objects directly, loop over database records and set the field's properties using column values. There are many ways to get data out of a database and those techniques are covered well all over the internet. Here is some code from one of my projects that builds a form based on records from a database table retrieved with Linq to Sql (fyi: this code doesn't use the current build):


    /// <summary>
    /// This class builds the DynamicForm object from data stored in the helpdesk database.
    /// </summary>
    public static class DynamicFormBuilder
        public static Form Build(int categoryId)
            var db = new HelpDeskDataContext();
            var dynForm = new Form();

            var fieldDefs = db.TicketFieldDefinitions.Where(f => f.CategoryID == categoryId);

            foreach (var fieldDef in fieldDefs)
                FieldType type = (FieldType)fieldDef.TypeID;
                Field dynField = null;

                switch (type)
                    case FieldType.CheckBox:
                        dynField = new CheckBox();
                    case FieldType.CheckBoxList:
                        dynField = new CheckBoxList();
                    case FieldType.RadioButtonList:
                        dynField = new RadioList();
                    case FieldType.TextArea:
                        dynField = new TextArea();
                    case FieldType.TextBox:
                        dynField = new TextBox();
                    case FieldType.Select:
                        dynField = new Select();
                    case FieldType.Literal:
                        dynField = new Literal { Wrap = true };
                        ((Literal)dynField).Html = fieldDef.Prompt;
                        throw new ApplicationException("Invalid dynamic field type ID: " + fieldDef.TypeID);


                // set properties common to all dynamic Fields
                dynField.DisplayOrder = fieldDef.Ordinal;                
                dynField.FieldWrapperClass = "DynFieldWrapper";

                if (dynField is InputField)
                    var inputField = (InputField)dynField;
                    inputField.PromptClass = "DynFieldPrompt";
                    inputField.ErrorClass = "DynFieldError";
                    inputField.Key = fieldDef.ID.ToString();
                    inputField.AddHtmlAttributes(fieldDef.HTMLOptions, "|", "=");
                    inputField.ValueDictionary["HtmlInput"] = fieldDef.HtmlInput;

                    inputField.Required = fieldDef.IsRequired;

                    if (!string.IsNullOrEmpty(fieldDef.RequiredError))
                        inputField.RequiredMessage = fieldDef.RequiredError;
                    inputField.Prompt = fieldDef.Prompt;                    

                    // set properties for list fields
                    if (inputField is ListField)
                        var lstField = (ListField)inputField;
                        lstField.AddChoices(fieldDef.Choices, Environment.NewLine);

                        if (lstField is OrientableField)
                            var orField = (OrientableField)lstField;
                            orField.Orientation = fieldDef.OrientHorizontal ? Orientation.Horizontal : Orientation.Vertical;

                    // set properties for text fields
                    if (inputField is TextField)
                        var txtField = (TextField)inputField;
                        txtField.RegularExpression = fieldDef.RegularExpression;
                        if (!string.IsNullOrEmpty(fieldDef.RegexError))
                            txtField.RegexMessage = fieldDef.RegexError;


            return dynForm;

Jul 29, 2010 at 6:10 AM

Hi Ronnie,

thanks for your quick response! From a first look, I think this is what I have been looking for. Hope I can get it to work with the current library.



Sep 15, 2010 at 12:59 PM

Hi Ronnie,

Just wanted to let you know that the code example that you posted has been a great help. The changes needed to get it to work with the current library weren't all that big.

Thanks for your help.

Sep 15, 2010 at 1:01 PM

Great! I'm glad that it helped.