tags:

views:

26

answers:

2

I have a problem using Jquery.

I do this:

$("#ID_ESTADO").change(function() {  
    document.getElementById("inprogress").style.visibility = "visible";  
    document.getElementById("ID_CERTIFICADO").setAttribute("disabled", true);  
    var url = '<%= Url.Content("~/") %>' + 'Certificado/ObtenerCertificados/';  
    $.getJSON(url + $("#ID_TIPO_POLIZA").val() + "/" + $("#ID_POLIZA").val() + "/" + $("#ID_ESTADO").val(), null, function(data) {  
        $("select[id$=ID_CERTIFICADO] > option").remove();  
        $.each(data, function(index, item) {  
            $("< option > ").attr("value", item.IDCertificado).text(item.IDCertificado).appendTo("#ID_CERTIFICADO");  
        });  
        document.getElementById("inprogress").style.visibility = "hidden";  
        document.getElementById("ID_CERTIFICADO").removeAttribute("disabled");  
    });  
});

Code Behind:

Function ObtenerCertificados(ByVal parID1 As Long, ByVal parID2 As Long, ByVal parID3 As Long) As ActionResult  
  Dim varResultado As JsonResult = Nothing  
  Dim varCertificados As IEnumerable(Of CertificadosVehiculos) = varServicio.ListarCertificados(parID1, parID2, parID3, True)  
  Dim varJsonCertificados As IEnumerable(Of CertificadosVehiculos) = From vari In varCertificados Select New CertificadosVehiculos With {.IDCertificado = vari.IDCertificado}  
  varResultado = Json(varJsonCertificados.ToList())  
  varResultado.JsonRequestBehavior = JsonRequestBehavior.AllowGet  
  Return varResultado  
End Function

If I change the dropdownlist ID_ESTADO then the dropdownlist ID_CERTIFICADO loads again. Sometimes this works perfectly, but when codebehind (varResultado) has many results (sometimes 3000) the lines inside jquery function doesn't run (I mean this lines):

$("select[id$=ID_CERTIFICADO] > option").remove();  
$.each(data, function(index, item) {  
  $("< option >").attr("value", item.IDCertificado).text(item.IDCertificado).appendTo("#ID_CERTIFICADO");  
});

I'm using jquery 1.4.2 and I tried with 1.4.1. I hope you can help me solving this. In CSharp o VB it's good the response. Sorry about my english.

A: 

The problem you are seeing is probably caused because the $.getJSON is an asynchronous call, and the statements following it are executed immediately, without waiting for the .$getJSON to complete.

Therefore, especially when there is a large result set, there could be a mismatch in what is expected.

Suggest you include the latter statements within the $.getJSON success function. Alternatively, call the $.getJSON using a $.ajax call specifying async = false.

Clicktricity
A: 

Ok. I solve this problem.
First in aspx I added:

$.ajaxSetup({  
    "error": function(XMLHttpRequest, textStatus, errorThrown) {  
        //alert(textStatus);  
        //alert(errorThrown);  
        alert(XMLHttpRequest.responseText);  
    );  
    }  
})  

Then I knew the error: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property

So I changed the Code Behind to this:

Function ObtenerCertificados(ByVal parID1 As Long, ByVal parID2 As Long, ByVal parID3     As Long) As ActionResult
    Dim varCertificados As IEnumerable(Of CertificadosVehiculos) = varServicio.ListarCertificados(parIDTipoPlaca, parNumeroPlaca, parIDVehiculo, True)
    Dim varJsonCertificados As IEnumerable(Of CertificadosVehiculos) = From vari In varCertificados Select New CertificadosVehiculos With {.IDCertificado = vari.IDCertificado}

    Dim varSerializer As New JavaScriptSerializer()
    varSerializer.MaxJsonLength = Int32.MaxValue
    Dim varResultado As New ContentResult()
    varResultado.Content = varSerializer.Serialize(varJsonCertificados)
    varResultado.ContentType = "application/json"
    Return varResultado
End Function

And that's it. Int32.MaxValue default value it's the problem
I hope this helps someone else.

Sebastián