0
I have a screen where I have the registration of some items, in my control I am receiving the data of the array, but it is coming empty.
I already tried some options here on the forum, such as "JSON.stringify" and "JSON.parse".
Image of the reading of the data:
on my controller, I’m getting the data like this:
public IActionResult SaveOrder( string[] order)
{
string result = order.ToString();
return Json(result);
}
<script>
function ExibirModalNovoProduto() {
$("#ModalNovoProduto").modal('show');
};
// Adicionar pedido
$("#addToList").click(function (e) {
e.preventDefault();
if ($.trim($("#productName").val()) == "" || $.trim($("#price").val()) == "" || $.trim($("#quantity").val()) == "") return;
var productName = $("#productName").val(),
price = $("#price").val(),
quantity = $("#quantity").val(),
detailsTableBody = $("#detailsTable tbody");
var productItem = '<tr><td>' + productName + '</td><td>' + quantity + '</td><td>' + price + '</td><td>' + (parseFloat(price) * parseInt(quantity)) + '</td><td><a data-itemId="0" href="#" class="deleteItem">Remove</a></td></tr>';
detailsTableBody.append(productItem);
clearItem();
});
// Após adicionar um novo pedido na lista, limpar o formulário para Adicionar mais pedidos.
function clearItem() {
$("#productName").val('');
$("#price").val('');
$("#quantity").val('');
};
// Depois de adicionar um novo pedido à lista, se desejar, você pode removê-lo.
$(document).on('click', 'a.deleteItem', function (e) {
e.preventDefault();
var $self = $(this);
if ($(this).attr('data-itemId') == "0") {
$(this).parents('tr').css("background-color", "#ff6347").fadeOut(800, function () {
$(this).remove();
});
}
});
// Coletar lista de pedidos múltiplos para o passe ao controlador
$("#saveOrder").click(function (e) {
e.preventDefault();
var orderArr = [];
orderArr.length = 0;
$.each($("#detailsTable tbody tr"), function () {
orderArr.push({
productName: $(this).find('td:eq(0)').html(),
quantity: $(this).find('td:eq(1)').html(),
price: $(this).find('td:eq(2)').html(),
amount: $(this).find('td:eq(3)').html()
});
});
var dadosrecebido = JSON.stringify({
name: $("#name").val(),
address: $("#address").val(),
order: orderArr
});
$.when(saveOrder(data)).then(function (response) {
console.log(response);
}).fail(function (err) {
console.log(err);
});
});
// Após clicar no botão Salvar, passe toda a exibição de dados para o controlador para salvar o banco de dados
function saveOrder(dadosrecebido) {
console.log(dadosrecebido);
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: "/Orders/SaveOrder/?registros=" + dadosrecebido,
success: function (result) {
alert(result);
location.reload();
},
error: function () {
alert("Error!")
}
});
};
</script>
Now it’s more a tip bro... The post does not return result, it only returns a 200 or 404... If your request expects something in return it is get.
– Pedro
Getting a little tipsy now... Please read this wikipedia page on the Http protocol: https://pt.wikipedia.org/wiki/Hypertext_Transfer_Protocol
– Pedro
tried many options and did not succeed, I will keep trying a solution
– Harry
If you want to pass me your Discord you transmit your screen and I help you.
– Pedro
mine is itasouza#6189, I thank
– Harry
with your help everything worked out, already added a positive response
– Harry
Ai sim man! Just take a look at how Razor works... ajax is not necessary when using Asp . net.
– Pedro