在asp net mvc 4视图中无法访问列表属性

时间:2012-12-12 14:54:39

标签: c# asp.net .net asp.net-mvc asp.net-mvc-4

可能很简单,但无法找到原因

我无法在视图中访问列表属性:

如您所见,我有一个包含RelacionamentoNomeadoModel类列表的类 的型号:

    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; }
    }

现在我构建模型并填充RelacionamentoNomeadoModel类,以便稍后将其添加到包含List的类中 的控制器

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());

最后我试图访问rela.lista.idRelacionamento或任何其他财产。 我传递给视图后,我无法从控制器访问它们 查看

   @{
    List<tgpwebged.Models.SettingsModels.RelacionamentoListModel> relacionamentos = Model;

    foreach(var rela in relacionamentos) {
    rela.lista.
}

}

2 个答案:

答案 0 :(得分:0)

问题是您要传递给查看RelacionamentoNomeadoModel个对象的集合,而在视图中您希望模型是tgpwebged.Models.SettingsModels.RelacionamentoListModel的集合。 不知道为什么你需要RelacionamentoListModel课程,但我认为如果你使用这段代码将会正常工作:

@{
    List<RelacionamentoNomeadoModel> relacionamentos = Model;

    foreach(var rela in relacionamentos) {
       // DO SOMETHING
    }
}

答案 1 :(得分:0)

您将数据作为强类型模型传递给View,因此您需要在视图中添加以下行:

     @model ProjectName.Models.YourModelName