Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4598532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:24:45+00:00 2026-05-21T23:24:45+00:00

I have an existing ASP.NET MVC 2 application that I’ve been asked to extend.

  • 0

I have an existing ASP.NET MVC 2 application that I’ve been asked to extend. I am adding a new feature to the site where I generate an employee assessment form based on a dynamic list of questions retrieved from our HR system. I have everything working with the exception of validation and posting the responses back to the site. Here’s some details:

  1. I retrieve a list of “Questions” from our back-end system via a web service call.
  2. Each “Question” contains the text to display as well as the following settings:
    • The question Type (corresponds to textbox, textarea, radio button list or checkbox list)
    • If comments are allowed
    • If an answer is required
    • When applicable, the list of possible responses

To generate the form, I use a for-each loop over the list of Questions. I use the value of the QuestionType property to determine which partial view to render (one for each of the types). For example, if QuestionType == SingleChoice, that partial renders the choices as a radio button list. If comments are allowed for the question, I also render an additional textarea field to hold the user’s comments.

As I said, rendering the form is working fine but now I need to:

A. Enforce when an answer is required. I’m using DataAnnotations for validation everywhere else in the solution but since I’m not working against a static model, I don’t see how I can do that.

B. Post the results back to the site. For each question, there can be text entered into a textbox or textarea, a selected value for a radio button list or multiple selected values for a checkbox list. Plus, each question could also have additional text sent back in the form of a comment.

All of the examples that I’ve seen working with dynamic “lists” are only concerned with posting a single value for each field and it is always the same type (e.g. a list of textboxes). With the variations I have to support, plus the need to send back the entered/selected value(s) and a comment for each question, I’m stumped.

Any guidance is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:24:46+00:00Added an answer on May 21, 2026 at 11:24 pm

    I’ve just finished completing exactly the same task.

    I chose to write a custom model binder for my dynamic form object. The model binder pulled out a bunch of prefixed form keys for hidden fields which contained some delimited meta data about the question (i.e IsRequired, QuestionType, QuestionId etc etc)

    I’m using MVC3 but I think this should all work in MVC2.

    I created a ModelBinder like:

    public class DynamicFormModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            // Create the object to be bound to (I had a kind of form object
            // with a simple list of answer objects
            DynamicForm form = new DynamicForm(new List<Answer>());
    
            HttpRequestBase request = controllerContext.HttpContext.Request;
    
            var keys = request.Form.AllKeys.Where(k => k.StartsWith("MyFormsKeyPrefix_Meta_"));
            foreach (var key in keys)
            {
                // Loop over each question's meta data. Metadata will always be present 
                // even if the user hasn't selected an answer as it's a hidden field
    
                // TODO: Split the meta data and pull out IsRequired, QuestionType etc
    
                // TODO: Get all the posted form values for the question (these values 
                //       will come from textboxes, dropdowns, checkboxes etc)
                //       Use a prefix like: MyFormsKeyPrefix_Answer_{QuestionId}
                //       textboxes & dropdowns will only ever have one value 
                //       but checkboxes could have multiple
    
                // TODO: If it's a mandatory question then ensure there is at least
                //       one posted value that is not an empty string
    
                // If there is a validation error then add it to the model state
                bindingContext.ModelState.AddModelError(key, "Field is required");
    
                foreach(var answerHtmlName in answerHtmlNames)
                {
                    // TODO: Loop over each posted answer and create some kind of nice
                    //       Answer object which holds the QuestionId, AnswerId, AnswerOptionId 
                    //       and Value etc.
    
    
                    // Add the answer to the forms answers list
                    form.Answers.Add(answer);
                }
            }
    
            return form;
        }
    
    }
    

    I register the ModelBinder in Global.asax using the following:

    ModelBinders.Binders.Add(typeof(DynamicForm), new DynamicFormModelBinder());
    

    So, the action method that recieves the form post looks something like:

    public ActionResult ProcessForm(DynamicForm form) {
        if(ModelState.IsValid) 
        {
            DynamicFormService.Process(form);
    
            return RedirectToAction("TheHttpGetAction");
        }
        return TheHttpGetAction();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing ASP.NET web application that I'm converting to MVC 1.0. The
Curious what recommendations anyone has. I have an existing asp.net forms application that does
I have an existing ASP.NET MVC application with some sample data in the SQL
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I have heard that you can run an ASP.NET application and ASP.NET mvc application
I have an existing asp.net webforms project that uses Microsoft's Enterprise DAAB for the
We've got an existing ASP.NET web application that already uses a home-grown role based
I have an existing ASP.NET 2.0 website, stored in Team Foundation Server 2005. Some
I have an existing ASP.NET website which I would like to painlessly add CMS
For whatever reason, I have a lot of clients that have existing data that's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.