尝试从模型中获取特定属性时出现对象对象错误

时间:2020-02-13 16:38:32

标签: javascript asp.net-mvc asp.net-core

对于数据库中的每个项目,我需要显示一个特定的属性。但是,当我运行我的项目时,应该显示所述属性的内容,它显示了Object对象。我已经尝试过

 console.log(ocorrencia.azimute)

 JSON.stringify(ocorrencia.azimute)

这是我的代码:

 $.get("/api/IgnicoesAPI", function (data) {


        $.each(data, function (i, item) {


            var infowindow = '<div id="content" style="hight:700px; width:500px">' +
                '<div id="siteNotice">' +
                '</div>' +
                '<div id="bodyContent">' +
                '<p><b>Avaliação da Ocorrência:</b></p>' +
                // iterar todas as ocorrencias para tirar delas as imagens


                $.each(item.listaOcorrencias, function (o, ocorrencia) {

                    //ERROR - Object object
                    '<p>Azimute:' + ocorrencia.azimute + '</p>' 

                });

                +'<p></p>' +

            //MORE CODE
            //...


            $('#json map').append(marker);


        }); 
    }); 

重要的是要指出我的item.listaOcorrencias引用了我的模型上的一个名为ListaOcorrencias的属性,这是来自另一个模型的数据列表。这是模型:

公共类别的Ignicoes {

    public Ignicoes()
    {
        ListaOcorrencias = new HashSet<Ocorrencias>();

    }

  //Other attributes

    public virtual ICollection<Ocorrencias> ListaOcorrencias { get; set; }

}

2 个答案:

答案 0 :(得分:0)

如果您正在使用jQuery .each遍历页面元素,则需要使用jQuery的$()来引用该元素。例如:

$.each(item.listaOcorrencias, function (o, ocorrencia) {

// Assuming that 'azimute' is a property of the element
'<p>Azimute:' + $(ocorrencia).azimute + '</p>' 

});

如果要遍历数据数组而不是页面元素,则应使用普通的JS forEach()

item.listaOcorrencias.forEach( function (ocorrencia, o) {
   '<p>Azimute:' + ocorrencia.azimute + '</p>'
});

答案 1 :(得分:0)

这是一个工作示例:

1。型号:

public class Ignicoes
{
    public Ignicoes()
    {
        ListaOcorrencias = new HashSet<Ocorrencias>();
    }
    public int Id { get; set; }
    public string Ign { get; set; }
    public virtual ICollection<Ocorrencias> ListaOcorrencias { get; set; }
}
public class Ocorrencias
{
    public int Id { get; set; }
    public string Azimute { get; set; }
}

2。视图:

<div id="json">
</div>
@section Scripts
{
    <script>
        $.get("/api/IgnicoesAPI", function (data) {
            $.each(data, function (i, item) {
                console.log(item);           
                var infowindow = '<div id="content" style="hight:700px; width:500px">' +
                    '<div id="siteNotice">' +
                    '</div>' +
                    '<div id="bodyContent">' +
                    '<p><b>Avaliação da Ocorrência:</b></p>' +
                    '</div>' +
                    '<div id="' + i + '">' +
                    '</div>';

                $('#json').append(infowindow);

                $.each(item.listaOcorrencias, function (o, ocorrencia) {
                    console.log(ocorrencia.azimute)

                    $("#" + i).append('<p>Azimute:' + ocorrencia.azimute + '</p>');
                });        
            }); 
        }); 
    </script>
}

3.Controller:

public ActionResult<List<Ignicoes>> IgnicoesAPI()
{
    var model = new List<Ignicoes>()
    {
        new Ignicoes(){ 
            Id=1,Ign="I1", 
            ListaOcorrencias = new List<Ocorrencias>(){ 
                new Ocorrencias() { Id=1,Azimute="O1" }, 
                new Ocorrencias() { Id=2,Azimute="O2" }
            } },
        new Ignicoes(){
            Id=1,Ign="I2",
            ListaOcorrencias = new List<Ocorrencias>(){
                new Ocorrencias() { Id=3,Azimute="O3" },
                new Ocorrencias() { Id=4,Azimute="O4" },
                new Ocorrencias() { Id=5,Azimute="O5" }
            } },
    };
    return model;
}

4。结果:

enter image description here