3

Depending multiple choices done by a user in few steps I have to generate a form in a web page for the user.

In a database I had all the necessary stuff (regex validation of every form field, name, type etc.) I would like to know what could be the best way to autogenerate a form using MVC3.

Should I autogenerate a model, set the model of my views to dynamic, and inject some validation attributes to every property of my dynamic model?

How should I get the values on my post action?

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
Jose3d
  • 9,149
  • 6
  • 33
  • 54
  • Without knowing anything about your data model in detail, I would say that looking into display and edit templates would be a good bet. I've no time right now to make a proper answer, so maybe someone else can do that or you can work it out for yourself ;-) – Erik van Brakel Jul 29 '12 at 10:51
  • Take a look at this question/answer: http://stackoverflow.com/questions/1040509/advice-with-dynamic-forms-in-asp-net-mvc – Steen Tøttrup Jul 29 '12 at 10:58

1 Answers1

0

As the fields are all dynamic (from the database), your model could very easily store an IEnumerable where Question is an object which has information about the type of field. i.e. Id, TypeId (text, checkbox, select list), Wording, Heading, ValidationTypeId etc. Then use mvchelpers passing in Question to a method which would determine the html to output. This could very well include a validator. On the form loop over Model.SurveyQuestions and for each row send Question to the mvc helper. The helper, knowing everything about the Question can output the label, type of input box and the required validator.

This is a wise way to accomplish what you are trying to do as your input fields is dynamic. I just completed a project doing exactly this.

MrMVCMan
  • 490
  • 2
  • 7
  • 20