0
That is the mistake:
Uncaught Typeerror: Cannot read Property '0' of Undefined
Well, here’s the thing:
I do a search on a WS. On the return of this search, I mount an html and play in my view (cshtml). Well, this works.
Then I made some filters to search inside my Session, that information. Let’s say in my Session, I have 29 Hotels. There, by the name filter of the hotel (a Textbox) I select a Hotel (There is an auto complete, which is working). I select the hotel. No OnBlur
of textbox
, i call a Jquery function to load the chosen hotel into the textbox.
It loads and at that moment, I take the same html that works on the first load, gives this error. I no longer know what to do. The code of the functions that carry html are large, so I didn’t post them, but I can do it if you want.
My searchResult giving error in the search
[HttpPost]
public JsonResult FiltroGeral(string[] refeicoes, string hotel, string[] categoria)
{
OfferV2[] pesquisaHotel = ((OfferV2[])SessaoUtil.Recuperar("PegaHotelPacote"));
List<object> searchResult = new List<object>();
if (refeicoes != null)
{
pesquisaHotel = pesquisaHotel.Where(x => refeicoes.Contains(x.CategoryId)).ToArray();
}
if (hotel != "")
{
pesquisaHotel = pesquisaHotel.Where(x => hotel.Contains(x.ProductName)).ToArray();
}
if (categoria != null)
{
pesquisaHotel = pesquisaHotel.Where(x => categoria.Contains(x.CategoryId)).ToArray();
}
searchResult.Add(pesquisaHotel);
return Json(new { searchResult }, JsonRequestBehavior.AllowGet);
}
This is my jquery
function FiltroGeral() {
var Refeicoes = [];
var Hotel = [];
var Categoria = [];
var str;
var filtro;
var cont = 1;
var count = 1;
var camas = [];
var strHotel;
var strCategoria;
var valor = 0;
var menorValor = 0;
var parcelas;
var qQuartos;
dados = []
for (var i = 0; i < filtroPesquisa.chkcategoria.length; i++) {
if (filtroPesquisa.chkcategoria[i].checked) {
Categoria.push(filtroPesquisa.chkcategoria[i].value);
}
}
for (var i = 0; i < filtroPesquisa.chkrefeicao.length; i++) {
if (filtroPesquisa.chkrefeicao[i].checked) {
Refeicoes.push(filtroPesquisa.chkrefeicao[i].value);
}
}
$.ajax({
url: '/Hotel/FiltroGeral',
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({ refeicoes: Refeicoes, hotel: $("#txtNomeHotel").val(), categoria: Categoria, Min: $("#ValorMin").val().replace(".", "").replace(",00", ""), Max: $("#ValorMax").val().replace(".", "").replace(",00", "") }),
success: function (data) {
$(data.searchResult).each(function () {
alert(ProductName);
//menorValor = this.SubOfferGroups[0].AnswerOffersList[0].SalePrice.Value;
//$(this.SubOfferGroups).each(function () {
// $(this.AnswerOffersList).each(function () {
// menorValor = Math.min(this.SalePrice.DefaultValue, this.SalePrice.DefaultValue);
// });
//});
if ($('#ValorHotel').val() != 0) {
menorValor = menorValor.toFixed(2) / $('#ValorHotel').val();
parcelas = $('#ValorHotel').val() + ' x'
}
str += '<div class="conteudo"> ';//Div inicial geral, fechar após tudo
str += '<div class="grid_11 margin-left-clear">';
str += '<div>';
str += '<h1>';
str += this.ProductName;
str += ' </h1>';
str += '<div class="valor">' + parcelas + ' ' + menorValor.toFixed(2) + '</div>'; // Inserir o valor com o parcelamento aqui
str += '<p>' + this.ShortDescription + '</p>';
str += '</div>';
str += '</div>';
str += '<div class="grid_8 margin-right-clear">';
str += '<div class="img-detalhes-hotel">';
str += '<img src="' + this.ImageList[0].URL + '" width="290" height="178" />';
str += '</div>';
str += '</div>';
str += '<div class="grid_19 row margin-bottom-10">';
str += '<div class="tab-detalhes-produto">';
str += '<a href="#fotos-0' + cont + '">Fotos</a>';
str += '</div>';
str += '<div class="tab-detalhes-produto">';
str += '<a href="#mapa-0' + cont + '">Mapa</a>';
str += '</div>';
str += '<div class="tab-detalhes-produto">';
str += '<a href="#servicos-0' + cont + '">Servicos</a>';
str += '</div>';
str += '<div class="tab-detalhes-produto">';
str += '<a href="#selecionar-quartos-0' + cont + '">Selecionar apartamento</a>';
str += '</div>';
str += '</div>';
str += '<div id="fotos-0' + cont + '" class="grid_19 fotos">';
str += '<div class="css3slider">';
if (this.ImageList.length > 0) {
$(this.ImageList).each(function () {
str += ' <input type="radio" name="slide_switch" id="' + cont + 'id' + count + '" checked="checked" />';
str += ' <label for="' + cont + 'id' + count + '">';
str += ' <img src="' + this.URL + '" />';
str += ' </label>';
str += ' <img src="' + this.URL + '" width="390" height="262" />';
count++;
});
}
str += '</div>';
str += '</div>';
str += '<div id="mapa-0' + cont + '" class="grid_19 mapa-localizacao-hotel">';
str += '<div class="map-canvas" data-opt=' + ' {"txtLatitude":' + this.Latitude + ',"txtLongitude":' + this.Longitude + '}' + ' style="display: block;width: 750px;height: 300px;"></div>';
str += '</div>';
str += '<div id="servicos-0' + cont + '" class="grid_19 servicos-hotel">';
str += '<p>' + this.Description + '</p>';
str += '</div>';
str += '<div id="selecionar-quartos-0' + cont + '" class="grid_19 selecionar-quartos">';
str += '<div class="grid_18 margin-left-15">';
str += '<h1>Selecione seus quartos de acordo com sua preferência</h1>';
$(this.SubOfferGroups).each(function () {
$(this.AnswerOffersList).each(function () {
valor = this.SalePrice.Value;
if (valor == "") {
valor = 0;
}
if ($('#ValorHotel').val() != 0) {
valor = valor.toFixed(2) / $('#ValorHotel').val();
}
str += '<div class="display-table border-bottom-cinza">';
str += '<div class="display-table">';
str += '<div class="grid_8">';
str += '<div class="valor">';
str += '<input type="checkbox" value="1" />';
str += this.ProductName + ' ' + parcelas + ' ' + valor.toFixed(2);
str += '</div>';
str += '</div>';
str += '<div class="grid_5">';
str += '<select class="select-group">';
str += '<option>Qtd. quartos</option>';
for (var i = 1; i < $("#Quartos").val() + 1; i++) {
if (i == 1)
str += '<option>' + i + ' quarto</option>';
else
str += '<option>' + i + ' quartos</option>';
if (i == $("#Quartos").val())
break;
}
str += '</select>';
str += '</div>';
str += '<div class="grid_4 selecionar-quartos-adicionais">';
str += '<a href="#dados-adicionais-quarto-0' + cont + '">+ informações</a>';
str += '</div>';
str += '</div>';
str += '<div id="dados-adicionais-quarto-0' + cont + '" class="dados-adicionais-quarto">';
str += '<div class="grid_11">';
str += '<table class="table table-bordered table-hover">';
str += '<thead>';
str += '<tr>';
str += '<td>Pessoas</td>';
str += '<td>Data</td>';
str += '<td>Preço</td>';
str += '</tr>';
str += '</thead>';
var adt = $('#txtAdulto').val();
var cri = $('#txtCrianca').val();
str += '<tbody>';
str += '<tr>';
str += '<td>' + (adt > 0 ? adt + ' Adulto(s)' : "") + ' ' + (cri > 0 ? cri + ' Criança(s)' : "") + '</td>';
str += '<td>' + '' + '</td>';
str += '<td>' + this.SalePrice.Value.toFixed(2) + '</td>';
str += '</tr>';
str += '</tbody>';
str += '</table>';
str += '</div>';
str += '<div class="grid_4">';
str += '<p>aqui será inserido o texto referente a serviços inclusos e politica de cancelamento</p>';
str += '</div>';
str += '</div>';
str += '</div>';
});
});
str += '</div>';
str += '</div>';
str += '</div>';
dados.push(str);
cont++;
str = "";
$("#Pagination").pagination(dados.length, {
items_per_page: 5,
num_display_entries: 1,
num_edge_entries: 1,
callback: pageselectCallback
});
});
},
error: function (error) {
alert("erro");
}
});
}
Inform the line that gives error. Or just the trecha of the function that gives this error.
– Roger Barretto
str += '<div class="grid_8 margin-right-clear">'; str += '<div class="img-details-hotel">'; str += '<img src="' + (this.ImageList.length != 0 ? this.Imagelist[0].URL : 0) + 'width="290" height="178" />'; str += '</div>'; str += '</div>'; The error of da no this.Imagelist, by the way everything that comes in my searchResult, says that and Undefined
– pnet
this.Imagelist is undefined in this case. Check the context, maybe even the "this" reference is Undefined. What is the method that loads the context of this "this" ?
– Roger Barretto
If it were the
this
, then the mistake would beCannot read property 'ImageList' of undefined
... unless he has something likethis[0]
, there could be.– Miguel Angelo
You do: this.Imagelist[0]
– pnet
@pnet: Now yes, with all the information you can answer the question. Always post the code, or relevant snippets of the code to get help.
– Miguel Angelo