Arbaaz
Arbaaz

Reputation: 321

How to create dynamic TextBoxes in MVC3 and store it in SQL database?

I am a newbie, i read MVC Movie App tutorial and created an AddressBook based on that, i am using ADO.NET Entity DataModel for stroing values in Database table, as i do not know anyother way, i am storing following values in my table- Id (primary key auto) name gender phoneno(Heres the problem i want to add multiple phone numbers, i want to add text boxes on clicking "add" link )

not only i want to add text boxes on view but also store it in database table , how do i do that? REMEMBER i am a newbie keep it simple

Upvotes: 0

Views: 659

Answers (1)

Shyju
Shyju

Reputation: 218712

If you need more than one phone number for same Contact. It is a ONE to MANY relation ship. that means you need a seperate table to store your Phone numbers

I would create a new table called PhoneNumber like this structure

PHONE_NUMBER_ID (INT) PRIMARY KEY
PHONE_NUMBER (VARCHAR)
CONTACT_ID (INT) - Foreign key to the Contact table

Your sample data will look like

PHONE_NUMBER_ID     PHONE_NUMBER    CONTACT_ID
---------------     ------------     ----------
1                   734578956         1
2                   987546563         2
3                   987645643         2

This means Contact 1 has one phone number and contact 2 has 2 phone numbers.

Now you need a Collection property in your Contact class to store the PhoneNumbers

public class Contact
{
  public int ID { set;get;} 
  public string FirstName { set;get;}
  //Other contact related proerpties

  IList<string> PhoneNumbers { set;get;}

 public Contact()
 {
   if(PhoneNumbers==null)
       PhoneNumbers=new List<string>();
 }
}

Upvotes: 1

Related Questions