Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advice with Dynamic Forms in ASP.NET MVC

I am working on rendering a dynamic form in an ASP.NET MVC view that will meet these requirements:

  • Fields can be validated
  • State is preserved when the form is invalid

I am looking into creating a custom model binder to achieve this. I am generally planning to do this:

  1. Form fields are defined with these properties
    • Prompt (label next to fields)
    • Type (text, checkboxlist, radiolist, etc.)
    • Choices (for list fields)
    • IsRequired
    • RegularExpression (for text fields)
    • Display Options
    • Collection of field definitions are sent from the controller to the view
    • Fields are rendered into HTML and sent to the browser
    • Form is sent back to the server
    • A custom model binder binds the form to a collection of field definitions that now contains the submitted values
    • Each field is validated
    • If required -> must have a value
    • If RegEx -> must match
    • For each invalid field, an error message is added to modelstate
    • The controller decides what to do
    • If all fields are valid
      • Do whatever with the fields and their values
    • If 1 or more fields are invalid
      • Send the collection of fields back to the view
      • Render the fields again, with their previously attempted values
      • Show the validation summary

I'm not sure if I am doing this in the best or easiest way. Will this approach give me a lot of problems or even work? What can I do to improve upon it?

like image 298
Ronnie Overby Avatar asked Jun 24 '09 19:06

Ronnie Overby


1 Answers

I wrote a class library that basically does exactly what my psuedocode in my question describes. It works great.

EDIT:

I finally got around to cleaning up my class library. I have added some new features and created a fairly well documented demo web application.

All of this is hosted here on CodePlex. I hope this helps someone.

like image 91
Ronnie Overby Avatar answered Nov 04 '22 17:11

Ronnie Overby