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: Note: I am using the Entity Framework and the MVC standard Could someone help me?
In this case I don’t think you’re doing the match between the result of the consultation and the object
clsMarca
of the kindMarca
(I don’t even know if that’s possible, at least directly).– João Martins
And what my alternatives would be?
– Rafael
Assuming that the object
db
is the typeDbContext
, it would not be enough to makeList<clsModelo> lstModelo = db.clsModelo.ToList();
?– João Martins
I tried that way, but it didn’t do much good
– Rafael
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ão Martins
@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
– Rafael