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 594539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:57:52+00:00 2026-05-13T15:57:52+00:00

I have a form with various inputs. I have a bunch of optional parameters

  • 0

I have a form with various inputs. I have a bunch of optional parameters that have some number of choices. I’d like to allow the user to select these optional parameters in the following way:

First, the user clicks the Add Component button at the bottom of the form and two new dropdowns appear above the button. The first dropdown has a list of Types that can be selected and the second one will be disabled. When the user selects a valid choice in the first dropdown, I want to populate the second dropdown with some Values that are specific to the specified Type. The user should be able to continue adding new Components (the pair of dropdowns) until all the desired optional Components are added. Ideally the form wouldn’t be posted until all the fields have been filled out and the desired Components added.

My question is this: How do I design this so that when the form is submitted and there are errors, that the dynamically added fields (the Components) will remain on the page and display the correct values?

I was planning on having the Add Component button be an Ajax.ActionLink that retrieves a partialview:

<div id="divComponentHolder"></div>
<%= Ajax.ActionLink("Add a Component", "GetComponentSelector", new AjaxOptions { UpdateTargetId = "divComponentHolder", InsertionMode = InsertionMode.InsertAfter}) %>

This partial view would look something like this:

   <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MVCAndWebFormsTest.Models.ComponentSelectorModel>" %>
   <%= Html.Encode("Type:")%>
   <%= Html.DropDownList("ComponentType", Model.ComponentTypes, "", new {onchange = "updateCompValues(this);"}) %>
   <%= Html.Encode("File/Folder:")%>
   <div id="selectdiv">
       <% Html.RenderPartial("ComponentValueSelector", Model.ComponentValues); %>
   </div> 
   <br/>
   <script type="text/javascript" language="javascript">
        function updateCompValues(obj) {
            $.ajax({
                url: <% Url.Action("GetCompValues") %>,
                async: true,
                type: 'POST',
                data: { type: obj.value },
                dataType: 'text',
                success: function(data) { $("#selectdiv").html(data); },
                error: function() {
                    console.log('Erreur');
                }
            });
        }
   </script>

And the ComponentValueSelector partial would be pretty simple:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MVCAndWebFormsTest.Controllers.ViewModels.ComponentValueModel>" %>
    <%= Html.DropDownList("CompValue", Model.SelectList) %>
  • 1 1 Answer
  • 1 View
  • 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-13T15:57:52+00:00Added an answer on May 13, 2026 at 3:57 pm

    Take a look at submitting list in MVC, here are a few useful sites:

    • http://blogs.teamb.com/craigstuntz/2009/02/11/38013/
    • http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
    • http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

    This is useful for submitting your dynamic DOM you are building up.

    Another way instead of making an ajax call to render a partial view you could always directly add elements to the DOM with jquery. For example use the jquery clone ( $(‘element’).clone(); ) method that would copy your list boxes then do some regex to change the id’s of the input boxes so they have unique id/names.

    As you are passing through a List of these ‘choices’ to your controller, you would then have to set them back in your Model and have your View iterate through them to display the correct amount of choices added.

    Here is a bare bones example. This may not be the best implementation for yourself or someone else may have better ideas.

    View

    <% for (int i = 0; i < in Model.Results.Count; i++) { %>
        //render better HTML but you should get the point!
        <%= Html.Hidden("choices[" + i + "].ID", i) %>
        <%= Html.DropDownList("choices[" + i + "].Choice1", ...) %>
        <%= Html.DropDownList("choices[" + i + "].Choice2", ...) %>
    <% } %>
    
    - add button
    

    jQuery

    $('#addButton').click(function()
    {
        //say if your choice drop downs were in a table then take the last
        //row and clone it
        var row = $('table tr:last').clone(true);
        var newId = //work out the new id from how many rows in the table
    
        //make sure to update the id and name parameters of inputs 
        //of the cloned row
        row.find(':input')
        .attr('id', function()
        {
           return $(this).attr('id').replace(/\[[\d+]\]/g, '[' + newlId + ']');
           //this replaces the cloned [id] with a new id
        })
        .attr('name', function()
        {
           return $(this).attr('name').replace(/\[[\d+]\]/g, '[' + newId + ']');
        });
    
        row.find(':hidden').val(newId); //update the value of the hidden input
    
        //alert(row.html()); //debug to check your cloned html is correct!
        //TODO: setup ajax call for 1st drop down list to render 2nd drop down
    
        $('table tr:last').after(row);//add the row
    
        return false;
    });
    

    Controller

    public ActionResult YourMethod(IList<YourObject> choices, any other parameters)
    {
       YourViewModel model = new YourViewModel();
       model.Results = choices; //where Results is IList<YourObject>
    
       return View(model);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have various inputs, selects and textareas in a form. I'd like to select
I have a form with some text-inputs: login, password. If user sees this form
I have 1 select, 2 text inputs & some JSON data in a form:
I have am HTML form in a PHP page. The form has various inputs.
Say we have a form where the user types in various info. We validate
We have a C# win Form app called Installer that silently installs various 3rd
Javascript I have code that will hide various sections in a MS CRM form
I have form, where some fields are looks like rows, so I can add/delete
I need to create an input form that will allow a user to enter
I have a form with the three sub-groups of inputs (of various kinds). The

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.