Click here to Skip to main content
15,311,561 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<table id="dataTable" class="table main-table-reference mt-0 mb-0">
                                            <tr>
                                                <td><input value="1" id="Bolgeid" name="Bolgeid[]" /></td>
                                                <td><input value="01.01.2011" /></td>
                                                <td><select name="third[]" id="third1">
                                                        <option value="1">One</option>
                                                        <option value="2">Two</option>
                                                        <option value="3">3Two</option>
                                                        <option value="4">4Two</option>
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td><input value="2" id="Bolgeid" name="Bolgeid[]" /></td>
                                                <td><input value="01.01.2011" /></td>
                                                <td><select name="third[]" id="third1">
                                                        <option value="1">One</option>
                                                        <option value="2">Two</option>
                                                        <option value="3">3Two</option>
                                                        <option value="4">4Two</option>
                                                    </select>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td><input value="3" id="Bolgeid" name="Bolgeid[]" /></td>
                                                <td><input value="01.01.2011" /></td>
                                                <td><select name="third[]" id="third1">
                                                        <option value="1">One</option>
                                                        <option value="2">Two</option>
                                                        <option value="3">3Two</option>
                                                        <option value="4">4Two</option>
                                                    </select>
                                                </td>
                                            </tr>
                                        </table>







SaveALL

$(document).ready(function () {
$('#BtnSaveAll').click(function () { PostPlan(); });
});
function PostPlan() {

var PlanList = [];
var Plan = {};
var table = document.getElementById("dataTable");
var rowLength = table.rows.length;
for (var i = 0, row = table.rows[i]; i < rowLength; i++) {
row = table.rows[i];
Plan.Bolgeid = row.cells[0].childNodes[0].value;
Plan.Tarih = row.cells[1].childNodes[0].value;
Plan.Perid = row.cells[2].childNodes[0].value; // select option field
PlanList.push(Plan);
}

$.ajax({
url: '/Plan/SavePlan',
type: "POST",
data: JSON.stringify(PlanList),
contentType: "application/json",
dataType: "json"
});


public JsonResult SavePlan(List<tbl_sahaisplani> Plan)
{
using (MetrobusEntities db = new MetrobusEntities())
{
foreach (Tbl_SahaIsPlani pln in Plan)
{
db.Tbl_SahaIsPlanis.Add(pln);
}
db.SaveChanges();
}

bool Result = true;
return Json(Result);

}

What I have tried:

Why always saves the last row in my for loop
Posted
Updated 25-Jan-21 2:33am
Comments
20212a 25-Jan-21 7:09am
   
Debug
Richard MacCutchan 25-Jan-21 7:51am
   
Because that is what your code tells it to do. If you want different items then you need to create a new Plan object each iteration of the loop.

1 solution

Quote:
JavaScript
var Plan = {};
...
for (var i = 0, row = table.rows[i]; i < rowLength; i++) {
    row = table.rows[i];
    Plan.Bolgeid = row.cells[0].childNodes[0].value;
    ...
    PlanList.push(Plan);
}
You create a single object outside of the loop, and then keep overwriting the properties of that one object instance for each row in your table.

You need to create a separate object instance for each row in your source table:
JavaScript
var Plan;
for (var i = 0, row = table.rows[i]; i < rowLength; i++) {
    row = table.rows[i];
    Plan = {}; // Create a new object instance for each row
    Plan.Bolgeid = row.cells[0].childNodes[0].value;
    ...
    PlanList.push(Plan);
}
   

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