Reputation: 617
I have this ajax function that receive a json var.
$.ajax({
type: "POST",
url: "@Url.Action("FilterCheck","Operatore")",
datatype: "json",
traditional: true,
data: { 'mycheck': mycheck, 'idprot': idprot, 'id': '@Model.id' },
success: function (response) {
$('#external-events').empty(); // clear existing items
$.each(response, function (index, item) {
var div = $('<div class="col-lg-3"><div class="external-event"></div></div>'); // Create new element
div.text(item.id + ' ' + item.nome + ' ' + item.cognome); // Set inner text
$('#external-events').append(div); // add the new element
},
I would refresh this HTML div:
<div id='external-events'>
@foreach (HAnnoZero.Repositories.utente item in ViewBag.Utenti)
{
<div class='col-lg-3'><div class='external-event'>@item.id- @item.cognome @item.nome</div></div>
} </div>
With the jquery function "each" i do the loop for more element json. In this way don't work, write only the first div class='col-lg-3' and not the second. How I can do? Thank you
Upvotes: 0
Views: 840
Reputation: 223
your problem is here :
div.text(item.id + ' ' + item.nome + ' ' + item.cognome); // Set inner text
you declared your div with col-lg-3 class with another div external-event inside it. when you use div.text(), jquery will replace the content of col-lg-3, and not external-event.
you should do :
div.find('.external-event').text(item.id + item.nome + item.cognome);
EDIT Or directly :
var div = $('<div class="col-lg-3"><div class="external-event">' + item.id + ' ' + item.nome + ' ' + item.cognome + '</div></div>'); // Create new element
$('#external-events').append(div); // add the new element
Upvotes: 1