c# - Getting List of table and saving it to another table -
so got rows , data table using jquery , ajax:
$("#btncopy").on("click", function () { //if ($tblrows.length !== 0) { var $tblrows = $("#tablerows>tr"); var maindata = {}; var detaildata = []; $.each($tblrows, function (index, elem) { maindata.id = $(elem).find(".id").html(); maindata.name = $(elem).find(".name").html(); maindata.organization = $(elem).find(".organization").html(); maindata.role_of = $(elem).find(".role_of").html(); maindata.addedby = $(elem).find(".addedby").html(); maindata.addedbyorg = $(elem).find(".addedbyorg").html(); maindata.dated = $(elem).find(".dated").html(); maindata.status_of = $(elem).find(".status_of").html(); console.log(elem); detaildata.push(maindata); maindata = {}; }); $.ajax({ contenttype: 'application/json; charset=utf-8', datatype: 'json', type: 'post', url: 'employeeaddnew', data: json.stringify({ detaildata: detaildata }), success: function (data) { }, failure: function (response) { $('#result').html(response); } }); // } });
i getting data controller in object:
(sorry using mac wasn't able take screenshot)
the question how save table i'm stuck, have no idea go here i'm new mvc , i'm still learning. how save table?
to save on table, create instance of object, populate properties list, add dbcontext, save (assuming organization table or class):
foreach (var item in detaildata) { organization obj = new organization() { addedby = item.addedby, organization = item.organization //populate other properties here. } _dbcontext.organization.add(obj); _dbcontext.savechanges(); }
take note saving different updating. if want update, remove add functionality, update property call savechanges.
Comments
Post a Comment