Might be simple but can´t find why
I cannot access list attributes in my view:
As you can see, I have a class that contains a list of RelacionamentoNomeadoModel class
Model:
public class RelacionamentoNomeadoModel
{
public int idRelacionamento { get; set; }
public string nomeTipoRight { get; set; }
public string nomeTipoLeft { get; set; }
}
public class RelacionamentoListModel
{
public List<RelacionamentoNomeadoModel> lista { get; set; }
}
Now I build the model and populate the RelacionamentoNomeadoModel class to later add it to the class containing the List
Controller
var relacionamentoObj = from r in context.sistema_relacionamento
join d in context.sistema_DocType on r.idTipoLeft equals d.id
select new tgpwebged.Models.SettingsModels.RelacionamentoNomeadoModel
{
idRelacionamento = r.id,
nomeTipoLeft = d.tipoName,
nomeTipoRight = d.tipoName
};
return PartialView(relacionamentoObj.ToList());
And the last I am trying to acces rela.lista.idRelacionamento or any other property.
I am able to access them from the controller bu not after I pass to the view
View
@{
List<tgpwebged.Models.SettingsModels.RelacionamentoListModel> relacionamentos = Model;
foreach(var rela in relacionamentos) {
rela.lista.
}
}
The problem is that you are passing to the View a collection of
RelacionamentoNomeadoModelobjects, while in the view you expect that the Model be a collection oftgpwebged.Models.SettingsModels.RelacionamentoListModel.Don’t know why you need
RelacionamentoListModelclass, but I think that if you use this code will be working fine: