Property is coming as Undefined

Asked

Viewed 90 times

0

In the controller I have a lambda that returns me three fields. When I run in jquery it tells me that the property is Undefined. I think it’s the way I try to take the amount. Can someone give me a hand there?

controller:

[HttpPost]
public JsonResult CarregaDadosPagina(int _nivel)
        {
            RupturaEntities db = new RupturaEntities();

            UsuarioNivel us = new UsuarioNivel();

            var result_carrega_pagina = db.Usuario
                .Where(n => n.IDUsuario == _nivel)
                .Select(s => new {s.NM_Usuario, s.Usuario1, s.Email }).ToList();

            return Json(result_carrega_pagina, JsonRequestBehavior.AllowGet);
        }  

Jquery:(Nm_usuario is Undefined)

function CarregaDados(ajaxParameter) {

    var str = "";

    $.ajax({

        url: '/CadastroAcesso/CarregaDadosPagina',
        datatype: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'POST',
        data: JSON.stringify({ _nivel: ajaxParameter }),
        success: function (data) {

            alert(data.result_carrega_pagina.NM_Usuario);
        },
        error: function (error) {

            alert(2);
        }
    })
}
  • If you are returning a list, you have to scroll through a . each to display data.result_carrega_pagina.NM_Usuario...

1 answer

2


@pnet, you are returning a list from your controller, and by making an alert from data.result_carrega_pagina.NM_Usuario gives undefined, because in fact property does not exist.

To give alert you can walk a .each with your data:

success: function (data) {
   $.each(data, function (index, itemData) {
       alert(itemData.NM_Usuario);
   });

}
  • 1

    Okay, solved. Let the 7 minutes pass and I already mark the answer. Thanks Cesar Miguel. I did each wrong. I did like this: $(date.result_carrega_pagina). each(Function (){})

Browser other questions tagged

You are not signed in. Login or sign up in order to post.