Property is coming as Undefined

0

In the controller I have a lambda that returns me three fields. When I squeeze in jquery it tells me that the property is undefined. I think that's the way I try to get value. Can someone help me 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);
        }
    })
}
    
asked by anonymous 05.11.2014 / 17:51

1 answer

2

@pnet, you are returning a list from your controller, and when you make an alert from data.result_carrega_pagina.NM_Usuario gives undefined , because the property does not actually exist.

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

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

}
    
05.11.2014 / 17:56