1

I have created label and text box and one add button and save button when i click on add button label and textbox will be added and when i click on save button data will added to database and only one name is saving (another dynamic created textbox values not inserting into database )what i have do enter all the textbox values my code is

In model.cs

         public void adddata(string Name)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
        SqlCommand cmd = new SqlCommand("addvalues", con);       
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", Name);
             con.Open();
             cmd.ExecuteScalar();
        con.Close();
    }

And in controller.cs is

 public Action Result Add(database db)
    {
        database objdb = new database();         
        objdb.adddata(db.Name);            
        return View();
    }

In the second pic i click on save button only first text box data is enter into database remaining not inserting then what i have to do to enter all text box values this is my output

meena
  • 245
  • 2
  • 12

1 Answers1

0

in your database class, define "Name" as list,array or any collection type. For example;

public class database{
    string[] Names;
}

and in view you must create textboxes with the same name, for example

<input type="text" name="Names" />
<input type="text" name="Names" />
...

MVC will bind them to your model (Mvc is using name conversation to bind data to model)

and controller

public ActionResult Add(database db)
{
    database objdb = new database();   

    for(...){
        objdb.adddata(db.Names[i]);  
    }     

    return View();
}
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197