Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Dear C# community

I want to save the values from an Array, I got the values from each table row in my View page using Ajax, I am passing an array to AddSteps in Home controller

passed values example for Array[] passRows = [["1","Kim","5"],["2","Tom","7"]]


C#
[HttpPost]

public JsonResult AddSteps(Array[] passRows, int processStepId)
{
  
 return Json( JsonRequestBehavior.AllowGet);
}


What I have tried:

I want to save each values of for each index 0 = 1,Kim,5 and 1= 2,Tom,7 in my tableA in database


C#
database class TableA
{
int Id;
int UserId
string Name;
int Level;
}

C#
[HttpPost]

public JsonResult AddSteps(Array[] passRows, int processStepId)
{
   foreach (var item in passRows)
    { 
       foreach (var itemtime in item)
         {
           Debug.Write(itemIndex.ToString());
           //what I know is that it writes each index value for each parent index
           // I want to save it like:

          tableA data = new tableA{
              UserId = itemIndex[0],
             Name = itemIndex[1],
             Level= itemIndex[2]
      }

       db.tableA.add(data);
       db.SaveChanges();
      //I dont know how to create the loop to do it 
     //please some help
    }
  }
 return Json( JsonRequestBehavior.AllowGet);
}


Thank you :)
Posted
Updated 4-Jul-19 19:03pm
v2

Assuming you've got row data like this:
string[,] rowdata = new[,] 
{
    {"1", "Kim", "5"}, {"2", "Tom", "7"}
};
You can add new rows like this:
public void AddSteps(string[,] passRows, int processStepId)
{
    for (int i = 0; i < passRows.GetLength(0); i++)
    {
        tableA.Rows.Add(passRows[i, 0], passRows[i, 1], passRows[i, 2]);
    }
}
 
Share this answer
 
hi Everyone
Thanks I solved by

C#
public void AddSteps(Array passRows, int processStepId)
{
    foreach(string[] indexN in passRows){
//setting string[] indexN  in passRows allows to identify the index position of 
// the array. As, it is received by type Array it helps making a reading of the
// content (Array passRows) with a type string[]

     var index  = indexN[0]//and soo on with other indexN values

       tableA data = new tableA{
        Name= indexN[1]  
      }
           db.tableA.Add(data);
           db.SaveChanges();
   }
}


:)
 
Share this answer
 
Comments
BillWoodruff 6-Jul-19 11:09am    
@JimB_ What you are doing is very strange: do you really want to create a new Table, and add that to the database, for each data-group?. imho, you need to add Rows to a Table that's already there in the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900