0
I have this AJAX function to update a table, it is this way:
function incluirFornecedor(idItem) {
var url = "/Produto/incluirFornecedorN";
$.ajax({
url: url
, data: { id: idItem }
, type: "POST"
, datatype: "html"
, success: function (data) {
$("#tabelaf").html("");
$(data).each(function () {
$("#tabelaf").append("<tr><td>" + data.resultado + "</td><td>" + data.ProdutoID + "</td></tr>");
});
}
});
}
And here is the function of the controller:
[HttpPost]
public ActionResult incluirFornecedorN(int id)
{
int produtoID = (db.Produtos.Max(a => a.Id) + 1);
var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == produtoID).Where(p => p.FornecedorID == id).ToList();
var item = new ProdutosFornecedores()
{
FornecedorID = id,
ProdutoID = (produtoID)
};
return Json(new { Resultado = item.FornecedorID, item.ProdutoID });
}
Only that it appears the id, I wanted it to appear the name, same is in the table, follow the HTML:
<table class="table table-responsive table-hover" id="tabelaf">
<tbody>
@foreach (var item in Model.ProdutosFornecedores)
{
<tr class="tr">
<td>@item.FornecedorProduto.Nome</td>
<td align="right">
<a class="link-excluir" href="#" data-id="@item.Id" title="Excluir"><i class="fa fa-trash-o fa-lg"></i></a>
</td>
</tr>
}
</tbody>
</table>
It brings the id, in Viewmodel, has the setting to appear the name correctly, only when adding by ajax, it does not appear.
In the
controlleryou’re just returningid. You need to add thenomein return for use in theview.– Thiago Magalhães