I have table
, which is filled by the bank and can be changed via ajax
, changed only in table
and unchanged in the bank.
What happens, is that as the data can be changed, I need to send the changed new data to save, I'm passing it this way:
$('#Editar').submit(function (e) {
e.preventDefault();
var id = document.getElementById("nfseid").value;
var valores = [];
$('.item').each(function () {
var entidade = {
Id: parseInt($(this).children()[10].innerText),
Codigo: ($(this).children()[1].innerText),
Descricao: ($(this).children()[2].innerText),
UnMedida: ($(this).children()[3].innerText),
Qtd: ($(this).children()[4].innerText),
VlrUnitario: ($(this).children()[5].innerText),
Deducao: ($(this).children()[6].innerText),
DescCondicionado: ($(this).children()[7].innerText),
DescIncondicionado: ($(this).children()[8].innerText),
Total: ($(this).children()[9].innerText),
};
valores.push(entidade);
});
var obj = {};
obj.valores = valores;
var form = this,
$form = $(form);
$.ajax({
url: "/NFSe/SalvaNFSItens",
type: 'POST',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify(obj),
success: function (data) {
if (data.resultado == true) {
if ($('#Editar').valid()) {
$form.off('submit').submit();
}
}
}
})
})
However, how can I compare what has been changed to make update
, insert
, or delete
?
[HttpPost]
public async Task<IActionResult> SalvaNFSItens([FromBody] ObjetoNFSeItens obj)
{
ApplicationUser user = await _userManager.GetUserAsync(User);
var result = false;
foreach (var item in obj.valores)
{
var valor = db.NFSeItens.Where(a => a.Id == item.Id).Single();
valor.ProdutoId = item.ProdutoId;
valor.qtd = item.qtd;
valor.UnMedida = item.UnMedida;
valor.ValorUnitário = item.ValorUnitário;
valor.ValorTotal = item.ValorTotal;
valor.Descricao = item.Descricao;
valor.DescontoIncondicionado = item.DescontoIncondicionado;
valor.DescontoCondicionado = item.DescontoCondicionado;
valor.Deducao = item.Deducao;
db.Update(valor);
db.SaveChanges();
result = true;
}
return Json(new { resultado = result });
}
How to do this in foreach
??
I am trying to do this, however it is returning error in this line:
var valor = db.NFSeItens.Where(a => a.Id == item.Id).Single();