0

I'm writing an ASP.NET 3.5 app and I'm passing values from the web page to the code behind via AJAX.

The object I'm passing:

var queryData = {
    'FiltrarPorTexto': false,
    'Texto': "",
    'FiltrarPorCategorizacion': false,
    'FiltrarPorTipo': false,
    'Tipo': 0,
    'Pagina': 1
};

My AJAX call:

$.ajax({
    type: "POST",
    url: 'SDI_generararchivosbloomberg.aspx/ObtenerListadoInstrumentosJSON',
    data: '{ "datosPeticion": ' + JSON.stringify(queryData) + ' }',
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(response) {
        // ...
        },
    error: function(xhr, msg, msg2) {
        // ...
    }
});

My code-behind:

[WebMethod]
[ScriptMethod]
public static ObjetoRespuesta ObtenerListadoInstrumentosJSON(ObjetoFiltro datosPeticion)
{
    if (datosPeticion.Pagina < 0) return new ObjetoRespuesta
        {
            PaginaActual = 0,
            TotalPaginas = 0,
            TotalRegistros = 0,
            HtmlTabla = "Error: el número de página no puede ser menor a 0"
        };
    var elementosASaltar = (datosPeticion.Pagina - 1) * ELEMENTOS_POR_PAGINA;
    var listado = bdc.GetTable<INSTRUMENTOS_BLOOMBERG>().AsQueryable();
    if (datosPeticion.FiltrarPorNombre) listado = listado.Where(i => i.NEMO_INSTRUMENTO.Contains(datosPeticion.Nombre));
    if (datosPeticion.FiltrarPorCategorizacion) listado = listado.Where(i => !i.ESTADO_CATEGORIZACION);
    if (datosPeticion.FiltrarPorTipo) listado = listado.Where(i => i.TIPO_INSTRUMENTO == (insts.Find(r => r.Id == datosPeticion.Tipo)).Id);
    var cantidadTotal = listado.Count();
    if (cantidadTotal < 1) return new ObjetoRespuesta
        {
            PaginaActual = 0,
            TotalPaginas = 0,
            TotalRegistros = 0,
            HtmlTabla = "Error: No se encontraron elementos"
        };
    if (elementosASaltar > 0) listado = listado.Skip(elementosASaltar);
    listado = listado.Take(ELEMENTOS_POR_PAGINA);            
    return new ObjetoRespuesta
    {
        PaginaActual = datosPeticion.Pagina,
        TotalPaginas = (cantidadTotal / ELEMENTOS_POR_PAGINA),
        TotalRegistros = cantidadTotal,
        HtmlTabla = GenerarTablaHtml(listado)
    };
 }

My problem is, some fields are not being correctly replicated in the code-behind. For example, when the queryData object is:

queryData
{...}
    FiltrarPorTexto: true
    Texto: "PEN"
    FiltrarPorCategorizacion: false
    FiltrarPorTipo: false
    Tipo: 0
    Pagina: 1

What gets unserialized in the code-behind is:

datosPeticion
{myNamespace.ObjetoFiltro}
    FiltrarPorCategorizacion: false
    FiltrarPorNombre: false
    FiltrarPorTipo: false
    Nombre: null
    Pagina: 1
    Tipo: 0

and the weird thing is, this only happens with those fields. Sending different values for FiltrarPorCategorizacion or Pagina, for example, yield the correct behavior.

Does anyone know what can I do to find the cause/fix this?

Léster
  • 1,177
  • 1
  • 17
  • 39
  • 1
    Side note: Why such a strange code to construct request? `JQuery.ajax` can create JSON correctly already - `data:{ datosPeticion: queryData}` without all this questionable string manipulations. – Alexei Levenkov May 26 '14 at 22:53
  • 1
    .NET spits an _Invalid JSON primitive_ error back. Had to resort to [this](http://stackoverflow.com/questions/2445874/invalid-json-primitive-in-ajax-processing) to get it working. – Léster May 27 '14 at 13:09
  • Right... it will read JSON to object not write one.. sorry - wrong suggestion. – Alexei Levenkov May 27 '14 at 22:30

2 Answers2

1

Nevermind, this was a mistake as dumb as they can get. For those with the same problem, make sure your JS object and the .NET object's fields have the same name.

Léster
  • 1,177
  • 1
  • 17
  • 39
1

I think your myNamespace.ObjetoFiltro class is missing properties 'FiltrarPorTexto' and 'Texto' while your json object needs a 'Nombre' property.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44