Hemel
Hemel

Reputation: 31

Generate Asp.net MVC Model from the form fields dynamically

I am building a Web Application where i am creating a dynamic form where user can create different field to make his or her own form.Then i want the application build their model dynamically based on the form fields.At the end database table will be created from that generated model and field value will be inserted as table data.Any suggestion or solution is highly appreciated.

Upvotes: 1

Views: 1240

Answers (1)

Thewads
Thewads

Reputation: 5053

I personally would shy away from trying to create the database table dynamically, but take an approach where you store the fields in a table, and relate them to a form object. Something like this:

public Form
{
    [Key]
    public int FormId {get;set;}
    public string Name {get;set;}
    public IList<Field> {get;set;}
}

public Field
{
    [Key]
    public int FieldId {get;set;}
    public string FieldName {get;set;}
    public string FieldValue {get;set;}

    [ForeignKey("FormId")]
    public Form Form {get;set;}
    public int FormId {get;set;}
}

With this structure, creating a form would simply be a case of creating a Form object, and then you can just simply add as many Field objects to it as you wish. Each Field object can be named anything that you want, and would be unique to the Form it is associated to

Upvotes: 1

Related Questions