Problem in javascript with method coming from controller per session

0

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");
            }
        });
    }
    
asked by anonymous 04.04.2014 / 14:37

1 answer

1

If you do not know the exact error code (OP added error line) , what I can do is try to explain in what situations this error occurs.

If a variable is defined, but does not have a value in it, that is, it is undefined then it will not be possible:

  • read properties of this variable

  • Call methods in this variable

  • use indexes on this variable

  • use associative keys in this variable

Example of the condition where the error occurs

var array;
var valor = array[0];

Code review

Your code has some problems that you may not have noticed.

In C #:

  • The searchResult variable is a list of objects, and when you use the Add method, only one object is being added. This object being added is an array ... this means that element 0 of this list is an array of multiple objects, it's just like a list inside another list.

  • You call ToArray many times, which makes the code less efficient. The best would be to operate directly over an IQueryable or IEnumerable. Since your data source already returns an array, then we can use IEnumerable, and then apply all filters in succession, calling ToArray or ToList only once.

  • The action is marked with the HttpPost attribute, which prevents a GET from being made in this method. Then there is no reason to use the JsonRequestBehavior.AllowGet option.

  • Verifying the variable hotel versus "" is not sufficient because it could be null, or contain only whitespaces. So it's best to use string.IsNullOrWhiteSpace to do the verification.

How would the C # look:

[HttpPost]
public JsonResult FiltroGeral(string[] refeicoes, string hotel, string[] categoria)
{
    var pesquisaHotel = ((IEnumerable<OfferV2>)SessaoUtil.Recuperar("PegaHotelPacote"));

    if (refeicoes != null)
    {
        pesquisaHotel = pesquisaHotel.Where(x => refeicoes.Contains(x.CategoryId));
    }

    if (!string.IsNullOrWhiteSpace(hotel))
    {
        pesquisaHotel = pesquisaHotel.Where(x => hotel.Contains(x.ProductName));
    }

    if (categoria != null)
    {
        pesquisaHotel = pesquisaHotel.Where(x => categoria.Contains(x.CategoryId));
    }

    return Json(pesquisaHotel.ToList());
}

Having the code that way, in javascript it could do like this:

  • instead of using $(data.searchResult).each( could apply directly to data :

    $(data).each(function () {
    
  • In this function, this would be the object itself equivalent to OfferV2 ... the way it was before, this was not true, because of the problem I pointed to one list inside another list.

In short, the error is within the C # code, as it adds a list within the other:

searchResult.Add(pesquisaHotel);
    
04.04.2014 / 15:28