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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:30:23+00:00 2026-05-26T15:30:23+00:00

Lets say I have some view models set up as follows: public class User

  • 0

Lets say I have some view models set up as follows:

public class User
{
    public string Name {get; set;}
    public IList<Phone> Phones {get; set;}
}    
public class Phone
{
    public string Number {get; set;}
}

My view is setup as follows:

@(form){
<div>

@Html.EditorFor(model => model.Name)
<!-- Should be dynamic creation of phone numbers -->
<a id="AddBtnPhone">add</a>
<ul id="PhoneList">
</ul>

<input type="submit"/>
</div>
}
<script>

$(function(){
  $('#AddBtnPhone').click(function(){
    //What do I do here?
  });
});

</script>

How do I add phone numbers dynamically so when this form gets posted, they are all in the list collection? I have no idea how to setup my views.

Scenario:

When the use clicks add, a list item containing a textbox to accept a phone number is added to PhoneList. When submit is clicked, the model User is posted to the controller with 1 element in the Phones list that contains the entered phone number.

  • 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-26T15:30:23+00:00Added an answer on May 26, 2026 at 3:30 pm

    When you dynamically create the input elements, you need to mimic the way MVC names the form elements so the model binding kicks in.

    For example, if you had a list of 3 phone numbers and let MVC render them out, they would look like this:

    <input type="texbox" name="Phones[0].Number" />
    <input type="texbox" name="Phones[1].Number" />
    <input type="texbox" name="Phones[2].Number" />
    

    So you need to mimic that. Something like this:

    $('#AddBtnPhone').click(function(){
        var $phoneList = $('#PhoneList');
        var numberOfExistingElements = $phoneList.children('.phone').length;
        $phoneList.append('input')
           .attr('type','textbox')
           .attr('name', 'Phones[' + numberOfExistingElements+1 + '].Number')
           .addClass('phone');    
    });
    

    I made this up off the top of my head (untested), so it may not be 100% (such as zero-based indexing for the array) but you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
Lets say I have a set of model classes like this: public class Person
Lets say I have a view, myView, a view controller, myViewController, and some sort
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
I have a name in a database, lets say its DFectuoso and some legacy
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:
Lets say I have one row with three columns - some buttons on left
Lets say I have a simple 1D array with 10-20 entries. Some will be
Lets say I have a 3D model of room in some format. How can
Let's say I have a Domain Object like this: public class Product { public

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.