This is the error:
Uncaught TypeError: Can not read property '0' of undefined
Well what happens is as follows:
I search on a WS. In the return of this search, I mount an html and game in my view (cshtml). Well, that works.
Then I made some filters to search inside my session, this information. Let's say that in my session, I have 29 Hotels. Then, through the hotel name filter (a TextBox) I select a Hotel (There is an auto complete, which is working). I select the hotel. In OnBlur
of textbox
, I call a Jquery function to load the chosen hotel in the textbox.
It loads and at that point, I get the same html that works on the first one loaded, gives this error. I do not know what to do anymore. The code of the functions that load the html are great, so I did not post them, but I can do it if you want.
My searchResult that gives search error
[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");
}
});
}