c# - Adding a new row to jQuery datatable and posting data -
i've got datatable adding row this:
function addrow(controller) { var table = $('#' + controller + '_data_table').datatable(); var index = table.rows()[0].length; var previousrowdate = $("input[name='[" + (index - 1) + "].date']").val(); var userid = $("input[name='[" + (index - 1) + "].userid']").val(); var date = new date(previousrowdate.replace('-', '')); date.setdate(date.getdate() + 1); if (isnan(date)) { date.setdate(new date()); } var rownode = table .row.add(["<input type='text' class='form-control daterangepicker' name='[" + index + "].date' value='" + moment(date).format("d-mmm-yyyy") + "'/>", "<input type='text' class='form-control' name='[" + index + "].min'/>", "<input type='text' class='form-control' name='[" + index + "].max'/>", "<input type='hidden' name='[" + index + "].userid' value='" + userid + "'/>" ]) .draw() .node(); $(rownode) .css('color', 'red') .animate({ color: 'black' }); $(".daterangepicker").datepicker( { dateformat: "dd-m-yy", changemonth: true, changeyear: true, mindate: 0 }); }
this adds row, on button click post data datatable save db, this:
function update(controller) { var table = $('#' + controller + '_data_table').datatable(); var model = table.$('*').serializearray(); $.each(model, function (i, fd) { var value = fd.value; value = value.replace(/\</g, "<"); //for < value = value.replace(/\>/g, ">"); //for > fd.value = value; }); $.post("/" + controller + "/update", model) .done( function (data) { if (data.erroroccurred) { bootbox.alert(data.message); } else if (data.success) { bootbox.alert(data.message); $('#' + controller + '_update').prop('disabled', true); } $('#' + controller + '_details').html(data.view); }); }
server side code:
[httppost] public actionresult update(list<viewmodel> effort) { try { effort.foreach(item => { var effortindb = _context.targets.singleordefault(t => t.id == item.id); if (effortindb == null) return; effortindb.date = item.date; effortindb.min = item.min; effortindb.max = item.max; }); _context.savechanges(); return json(new { erroroccurred = false, message = "" }); } catch (exception exception) { return json(new { erroroccurred = true, message = $"an error occurred: {system.environment.newline}{exception.message}" }); } }
the issue having userid 0 on server side code, event though i've created hidden field value of it.
i need id save in db
Comments
Post a Comment