Doubt on the link of two tables to bring in view

Asked

Viewed 70 times

3

I have a Brand table and a Model table, where at the time of listing all brands I can bring from the bank and send to my controller, only at the moment I do the research to list the Templates and show in my view models and their respective brands, I can’t, the brands part is blank and I can only bring the models.

My class of Brand:

public class clsMarca
{
   [Key]
   public int marIdMarca { get; set; }

   [Required(ErrorMessage = "Informe o nome da marca")]
   [MinLength(3), MaxLength(50)]
   [Display(Name = "Marca")]
   public string marMarca { get; set; }

   public virtual ICollection<clsModelo> Modelos { get; set; }
}

My class of models:

public class clsModelo
    {
        [Key]
        public int modIdModelo { get; set; }

        [Required(ErrorMessage="Informe o nome do Modelo")]
        [Display(Name = "Modelo")]
        [StringLength(40, MinimumLength = 3, ErrorMessage = "O nome do modelo precisa ter no mínimo 3 letras")]
        public string modModelo { get; set; }

        [Display(Name = "Marca")]
        public int modIdMarca { get; set; }

        [ForeignKey("modIdMarca")]
        public virtual clsMarca Marca { get; set; }

        List<clsModelo> lstModelo { get; set; }
    }

My SQL command in the repository class to bring all templates from the database

public List<clsModelo> listar()
        {
            strQuery = "select * from tblModelo mode join tblMarca mar on mar.marIdMarca = mode.modIdMarca";
            List<clsModelo> lstModelo = db.Database.SqlQuery<clsModelo>(strQuery).ToList();
            return lstModelo;
        }

I tried with several ways to make the SQL command but none helped:

 //strQuery = "select modModelo, (select marMarca from tblMarca) from tblModelo";
 //strQuery = "select modModelo, modIdMarca from tblModelo";
 //strQuery = "select mode.modModelo, mar.marMarca from tblModelo mode left join tblMarca mar on mar.marIdMarca = mode.modIdMarca";

And always shows like this in the view: inserir a descrição da imagem aqui Note: I am using the Entity Framework and the MVC standard Could someone help me?

  • 1

    In this case I don’t think you’re doing the match between the result of the consultation and the object clsMarca of the kind Marca (I don’t even know if that’s possible, at least directly).

  • And what my alternatives would be?

  • 1

    Assuming that the object db is the type DbContext, it would not be enough to make List<clsModelo> lstModelo = db.clsModelo.ToList();?

  • I tried that way, but it didn’t do much good

  • But you didn’t return all the templates? Inside each template you have the object clsMarca that has the brand information. Edit your question and place the code you are using to create the list you have as an image (where the tag information does not appear).

  • @Joãomartins, in this way does not even return the models, in my case I am doing the research in the bank, he is bringing the models, but the brands come null, and I do not understand why this, and I can bring the Brand Id but I can not bring her name. Can you help me? I’m doing it through Database-First

Show 1 more comment

1 answer

2


In the foreach of his View, you need to call the marMarca from within the Marca of item of your foreach

Thus:

<td>
   @item.Marca.marMarca
</td>
  • 1

    Already this way in my foreach

  • 1

    in your way listar(), to marMarca populated? (If you don’t know how to see, put a break point on that line return lstModelo;, debug the code, when arriving on this line, click on the lstModelo and tighten Shift + F9)

Browser other questions tagged

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